|
1 | 1 | import { Request, Response } from "express"; |
2 | | -import { ErrorCode, UserRole, Tokens } from "../interfaces/enum"; |
| 2 | +import { ErrorCode } from "../interfaces/enum"; |
3 | 3 | import { |
4 | 4 | sendPasswordReset, |
5 | 5 | login, |
6 | 6 | updatePassword, |
7 | | - register, |
8 | 7 | validateRefreshToken, |
9 | 8 | loginWithGoogleLink, |
10 | 9 | loginWithGoogleVerify, |
11 | 10 | impersonate, |
12 | | - approveLocation |
| 11 | + approveLocation, |
| 12 | + verifyEmail |
13 | 13 | } from "../rest/auth"; |
14 | 14 | import { verifyToken } from "../helpers/jwt"; |
| 15 | +import { |
| 16 | + Get, |
| 17 | + Post, |
| 18 | + Controller, |
| 19 | + ClassMiddleware, |
| 20 | + Middleware |
| 21 | +} from "@overnightjs/core"; |
| 22 | +import { authHandler } from "../helpers/middleware"; |
15 | 23 |
|
16 | | -export const routeAuthVerifyToken = async (req: Request, res: Response) => { |
17 | | - const token = |
18 | | - req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
19 | | - const subject = req.body.subject; |
20 | | - if (!token || !subject) throw new Error(ErrorCode.MISSING_FIELD); |
21 | | - try { |
22 | | - const data = await verifyToken(token, subject); |
23 | | - res.json({ verified: true, data }); |
24 | | - } catch (error) { |
25 | | - throw new Error(ErrorCode.INVALID_TOKEN); |
| 24 | +@Controller("auth") |
| 25 | +export class AuthController { |
| 26 | + @Post("login") |
| 27 | + async login(req: Request, res: Response) { |
| 28 | + const email = req.body.email; |
| 29 | + const password = req.body.password; |
| 30 | + if (!email || !password) throw new Error(ErrorCode.MISSING_FIELD); |
| 31 | + res.json(await login(email, password, res.locals)); |
26 | 32 | } |
27 | | -}; |
28 | 33 |
|
29 | | -export const routeAuthLogin = async (req: Request, res: Response) => { |
30 | | - const email = req.body.email; |
31 | | - const password = req.body.password; |
32 | | - if (!email || !password) throw new Error(ErrorCode.MISSING_FIELD); |
33 | | - res.json(await login(email, password, res.locals)); |
34 | | -}; |
| 34 | + @Post("verify-token") |
| 35 | + @Middleware(authHandler) |
| 36 | + async postVerifyToken(req: Request, res: Response) { |
| 37 | + const token = |
| 38 | + req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
| 39 | + const subject = req.body.subject; |
| 40 | + if (!token || !subject) throw new Error(ErrorCode.MISSING_FIELD); |
| 41 | + try { |
| 42 | + const data = await verifyToken(token, subject); |
| 43 | + res.json({ verified: true, data }); |
| 44 | + } catch (error) { |
| 45 | + throw new Error(ErrorCode.INVALID_TOKEN); |
| 46 | + } |
| 47 | + } |
35 | 48 |
|
36 | | -export const routeAuthRefresh = async (req: Request, res: Response) => { |
37 | | - const token = |
38 | | - req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
39 | | - if (!token) throw new Error(ErrorCode.MISSING_TOKEN); |
40 | | - res.json(await validateRefreshToken(token, res.locals)); |
41 | | -}; |
| 49 | + @Post("refresh") |
| 50 | + @Middleware(authHandler) |
| 51 | + async postRefreshToken(req: Request, res: Response) { |
| 52 | + const token = |
| 53 | + req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
| 54 | + if (!token) throw new Error(ErrorCode.MISSING_TOKEN); |
| 55 | + res.json(await validateRefreshToken(token, res.locals)); |
| 56 | + } |
42 | 57 |
|
43 | | -export const routeAuthRegister = async (req: Request, res: Response) => { |
44 | | - const email = req.body.email; |
45 | | - const user = req.body; |
46 | | - delete user.organizationId; |
47 | | - delete user.email; |
48 | | - if (user.role == UserRole.ADMIN) delete user.role; |
49 | | - delete user.membershipRole; |
50 | | - if (!req.body.name || !email) throw new Error(ErrorCode.MISSING_FIELD); |
51 | | - await register( |
52 | | - user, |
53 | | - res.locals, |
54 | | - email, |
55 | | - req.body.organizationId, |
56 | | - req.body.membershipRole |
57 | | - ); |
58 | | - res.json({ success: true }); |
59 | | -}; |
| 58 | + @Post("reset-password/request") |
| 59 | + async postResetPasswordRequest(req: Request, res: Response) { |
| 60 | + const email = req.body && req.body.email; |
| 61 | + if (!email) throw new Error(ErrorCode.MISSING_FIELD); |
| 62 | + await sendPasswordReset(email, res.locals); |
| 63 | + res.json({ queued: true }); |
| 64 | + } |
60 | 65 |
|
61 | | -export const routeAuthResetPasswordRequest = async ( |
62 | | - req: Request, |
63 | | - res: Response |
64 | | -) => { |
65 | | - const email = req.body && req.body.email; |
66 | | - if (!email) throw new Error(ErrorCode.MISSING_FIELD); |
67 | | - await sendPasswordReset(email, res.locals); |
68 | | - res.json({ queued: true }); |
69 | | -}; |
| 66 | + @Post("reset-password/recover") |
| 67 | + async postResetPasswordRecover(req: Request, res: Response) { |
| 68 | + const token = |
| 69 | + req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
| 70 | + const password = req.body.password; |
| 71 | + if (!token || !password) throw new Error(ErrorCode.MISSING_FIELD); |
| 72 | + await updatePassword(token, password, res.locals); |
| 73 | + res.json({ success: true }); |
| 74 | + } |
70 | 75 |
|
71 | | -export const routeAuthResetPasswordRecover = async ( |
72 | | - req: Request, |
73 | | - res: Response |
74 | | -) => { |
75 | | - const token = |
76 | | - req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
77 | | - const password = req.body.password; |
78 | | - if (!token || !password) throw new Error(ErrorCode.MISSING_FIELD); |
79 | | - await updatePassword(token, password, res.locals); |
80 | | - res.json({ success: true }); |
81 | | -}; |
| 76 | + @Get("google/link") |
| 77 | + async getLoginWithGoogleLink(req: Request, res: Response) { |
| 78 | + res.json({ |
| 79 | + redirect: loginWithGoogleLink() |
| 80 | + }); |
| 81 | + } |
82 | 82 |
|
83 | | -export const routeAuthLoginWithGoogleLink = async ( |
84 | | - req: Request, |
85 | | - res: Response |
86 | | -) => { |
87 | | - res.json({ |
88 | | - redirect: loginWithGoogleLink() |
89 | | - }); |
90 | | -}; |
| 83 | + @Post("google/verify") |
| 84 | + async postLoginWithGoogleVerify(req: Request, res: Response) { |
| 85 | + const code = |
| 86 | + req.body.code || (req.get("Authorization") || "").replace("Bearer ", ""); |
| 87 | + if (!code) throw new Error(ErrorCode.MISSING_TOKEN); |
| 88 | + res.json(await loginWithGoogleVerify(code, res.locals)); |
| 89 | + } |
91 | 90 |
|
92 | | -export const routeAuthLoginWithGoogleVerify = async ( |
93 | | - req: Request, |
94 | | - res: Response |
95 | | -) => { |
96 | | - const code = |
97 | | - req.body.code || (req.get("Authorization") || "").replace("Bearer ", ""); |
98 | | - if (!code) throw new Error(ErrorCode.MISSING_TOKEN); |
99 | | - res.json(await loginWithGoogleVerify(code, res.locals)); |
100 | | -}; |
| 91 | + @Post("impersonate/:id") |
| 92 | + @Middleware(authHandler) |
| 93 | + async getImpersonate(req: Request, res: Response) { |
| 94 | + const tokenUserId = res.locals.token.id; |
| 95 | + const impersonateUserId = req.params.id; |
| 96 | + if (!tokenUserId || !impersonateUserId) |
| 97 | + throw new Error(ErrorCode.MISSING_FIELD); |
| 98 | + res.json(await impersonate(tokenUserId, impersonateUserId)); |
| 99 | + } |
101 | 100 |
|
102 | | -export const routeAuthImpersonate = async (req: Request, res: Response) => { |
103 | | - const tokenUserId = res.locals.token.id; |
104 | | - const impersonateUserId = req.params.id; |
105 | | - if (!tokenUserId || !impersonateUserId) |
106 | | - throw new Error(ErrorCode.MISSING_FIELD); |
107 | | - res.json(await impersonate(tokenUserId, impersonateUserId)); |
108 | | -}; |
| 101 | + @Get("approve-location") |
| 102 | + @Middleware(authHandler) |
| 103 | + async getApproveLocation(req: Request, res: Response) { |
| 104 | + const token = |
| 105 | + req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
| 106 | + if (!token) throw new Error(ErrorCode.MISSING_FIELD); |
| 107 | + res.json(await approveLocation(token, res.locals)); |
| 108 | + } |
109 | 109 |
|
110 | | -export const routeAuthApproveLocation = async (req: Request, res: Response) => { |
111 | | - const token = |
112 | | - req.body.token || (req.get("Authorization") || "").replace("Bearer ", ""); |
113 | | - if (!token) throw new Error(ErrorCode.MISSING_FIELD); |
114 | | - res.json(await approveLocation(token, res.locals)); |
115 | | -}; |
| 110 | + @Get("verify-email") |
| 111 | + async postVerifyEmail(req: Request, res: Response) { |
| 112 | + await verifyEmail(req.body.token || req.params.token, res.locals); |
| 113 | + res.json({ success: true }); |
| 114 | + } |
| 115 | +} |
0 commit comments