Added the /user/me route to show information about the logged in user

This commit is contained in:
Mathias Wagner 2023-07-25 21:56:39 +02:00
parent 5f6d41f4b7
commit 91386a158d
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

View File

@ -1,12 +1,22 @@
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { sendError, validateSchema } from "@utils/error"; import { sendError, validateSchema } from "@utils/error";
import { registerValidation, totpSetup, verificationValidation } from "./validations/account"; 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 { authenticate } from "@middlewares/auth";
import speakeasy from "speakeasy"; import speakeasy from "speakeasy";
const app: Router = Router(); 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) => { app.post("/register", async (req: Request, res: Response) => {
if (validateSchema(res, registerValidation, req.body)) return; if (validateSchema(res, registerValidation, req.body)) return;