From 91386a158d825e8a8e9dac20368b46c201c13b8a Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Tue, 25 Jul 2023 21:56:39 +0200 Subject: [PATCH] Added the /user/me route to show information about the logged in user --- src/routes/v1/account.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/routes/v1/account.ts b/src/routes/v1/account.ts index 79d674f..be168a4 100644 --- a/src/routes/v1/account.ts +++ b/src/routes/v1/account.ts @@ -1,12 +1,22 @@ import { Request, Response, Router } from "express"; import { sendError, validateSchema } from "@utils/error"; import { registerValidation, totpSetup, verificationValidation } from "./validations/account"; -import { createAccount, updateTOTP, verifyAccount } from "@controller/account"; +import { createAccount, generateAvatarUrl, updateTOTP, verifyAccount } from "@controller/account"; import { authenticate } from "@middlewares/auth"; import speakeasy from "speakeasy"; const app: Router = Router(); +app.get("/me", authenticate, async (req: Request, res: Response) => { + if (!req.user) return sendError(res, 400, 1091, "You are not authenticated."); + + if (!req.user.verified) + return sendError(res, 400, 1093, "Your account is not verified. Check your mails to verify."); + + res.json({id: req.user._id, username: req.user.username, email: req.user.email, + allowInvites: req.user.allowInvites, totpEnabled: req.user.totpEnabled, avatar: generateAvatarUrl(req.user.email)}); +}); + app.post("/register", async (req: Request, res: Response) => { if (validateSchema(res, registerValidation, req.body)) return;