feat: changed some user API routes

pull/190/head
tycrek 2 years ago
parent 3ef36dc2e7
commit 4740286368
No known key found for this signature in database
GPG Key ID: FF8A54DCE404885A

@ -55,12 +55,9 @@ const userFinder = (res: Response, user: User | undefined) => user ? res.json(us
function buildUserRouter() { function buildUserRouter() {
const userRouter = Router(); const userRouter = Router();
// Index // Index/Get all users
userRouter.get('/', (_req: Request, res: Response) => res.sendStatus(200));
// Get all users
// Admin only // Admin only
userRouter.get('/all', adminAuthMiddleware, (req: Request, res: Response) => res.json(users)); userRouter.get('/', adminAuthMiddleware, (req: Request, res: Response) => res.json(users));
// Get self // Get self
userRouter.get('/self', (req: Request, res: Response) => userRouter.get('/self', (req: Request, res: Response) =>
@ -72,8 +69,9 @@ function buildUserRouter() {
// Reset password (new plaintext password in form data; HOST SHOULD BE USING HTTPS) // Reset password (new plaintext password in form data; HOST SHOULD BE USING HTTPS)
// Admin only // Admin only
userRouter.post('/reset', adminAuthMiddleware, (req: Request, res: Response) => { // todo: user-resets using existing password
const id = req.body.id; userRouter.post('/password/reset/:id', adminAuthMiddleware, (req: Request, res: Response) => {
const id = req.params.id;
const newPassword = req.body.password; const newPassword = req.body.password;
setUserPassword(id, newPassword) setUserPassword(id, newPassword)
@ -83,7 +81,7 @@ function buildUserRouter() {
// Create a new user // Create a new user
// Admin only // Admin only
userRouter.post('/new', adminAuthMiddleware, (req: Request, res: Response) => { userRouter.post('/', adminAuthMiddleware, (req: Request, res: Response) => {
const username: string | undefined = req.body.username; const username: string | undefined = req.body.username;
const password: string | undefined = req.body.password; const password: string | undefined = req.body.password;
const admin = req.body.admin ?? false; const admin = req.body.admin ?? false;
@ -98,10 +96,6 @@ function buildUserRouter() {
.catch((err) => errorHandler(res, err)); .catch((err) => errorHandler(res, err));
}); });
// Get all users
// Admin only
userRouter.get('/all', adminAuthMiddleware, (req: Request, res: Response) => res.json(users));
// Get a user (must be last as it's a catch-all) // Get a user (must be last as it's a catch-all)
// Admin only // Admin only
userRouter.get('/:id', adminAuthMiddleware, (req: Request, res: Response) => userRouter.get('/:id', adminAuthMiddleware, (req: Request, res: Response) =>
@ -149,6 +143,7 @@ function buildUserRouter() {
// Sets a username // Sets a username
// Admin only // Admin only
// todo: allow users to change their own username
userRouter.put('/username/:id', adminAuthMiddleware, (req: Request, res: Response) => { userRouter.put('/username/:id', adminAuthMiddleware, (req: Request, res: Response) => {
const id = req.params.id; const id = req.params.id;
const username: string | undefined = req.body.username; const username: string | undefined = req.body.username;
@ -163,6 +158,7 @@ function buildUserRouter() {
// Resets a token // Resets a token
// Admin only // Admin only
// todo: allow users to reset their own token
userRouter.put('/token/:id', adminAuthMiddleware, (req: Request, res: Response) => { userRouter.put('/token/:id', adminAuthMiddleware, (req: Request, res: Response) => {
const id = req.params.id; const id = req.params.id;

Loading…
Cancel
Save