Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 3b6d2b8

Browse files
✨ Use decorators for all routes
1 parent 54bc37f commit 3b6d2b8

File tree

9 files changed

+494
-371
lines changed

9 files changed

+494
-371
lines changed

src/rest/email.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
getUserPrimaryEmailObject,
77
deleteEmail,
88
getUserEmails,
9-
checkIfNewEmail
9+
checkIfNewEmail,
10+
resendEmailVerification
1011
} from "../crud/email";
1112
import { createEvent } from "../crud/event";
1213
import {
@@ -28,11 +29,34 @@ export const getAllEmailsForUser = async (
2829
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
2930
};
3031

31-
export const addEmailToUser = async (
32+
export const getEmailForUser = async (
33+
tokenUserId: number,
34+
userId: number,
35+
emailId: number
36+
) => {
37+
if (await can(tokenUserId, Authorizations.READ, "user", userId))
38+
return await getEmail(emailId);
39+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
40+
};
41+
42+
export const resendEmailVerificationForUser = async (
43+
tokenUserId: number,
44+
userId: number,
45+
emailId: number
46+
) => {
47+
if (await can(tokenUserId, Authorizations.UPDATE, "user", userId))
48+
return await resendEmailVerification(emailId);
49+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
50+
};
51+
52+
export const addEmailToUserForUser = async (
53+
tokenUserId: number,
3254
userId: number,
3355
email: string,
3456
locals: Locals
3557
) => {
58+
if (await can(tokenUserId, Authorizations.UPDATE, "user", userId))
59+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
3660
validate(email, ValidationTypes.EMAIL);
3761
await checkIfNewEmail(email);
3862
await createEmail({ email, userId });
@@ -43,11 +67,14 @@ export const addEmailToUser = async (
4367
return;
4468
};
4569

46-
export const deleteEmailFromUser = async (
47-
emailId: number,
70+
export const deleteEmailFromUserForUser = async (
71+
tokenUserId: number,
4872
userId: number,
73+
emailId: number,
4974
locals: Locals
5075
) => {
76+
if (await can(tokenUserId, Authorizations.UPDATE, "user", userId))
77+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
5178
const email = await getEmail(emailId);
5279
if (email.userId != userId)
5380
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);

src/rest/organization.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,12 @@ export const getOrganizationRecentEventsForUser = async (
351351
return await getOrganizationRecentEvents(organizationId);
352352
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
353353
};
354+
355+
export const getOrganizationMembershipsForUser = async (
356+
userId: number,
357+
organizationId: number
358+
) => {
359+
if (await can(userId, Authorizations.READ, "organization", organizationId))
360+
return await getOrganizationMemberDetails(organizationId);
361+
throw new Error(ErrorCode.INSUFFICIENT_PERMISSION);
362+
};

src/routes/admin.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { Request, Response } from "express";
22
import { ErrorCode } from "../interfaces/enum";
33
import { getAllOrganizationForUser, getAllUsersForUser } from "../rest/admin";
4+
import { Get, Controller, ClassMiddleware } from "@overnightjs/core";
5+
import { authHandler } from "../helpers/middleware";
46

5-
export const routeAdminOrganizations = async (req: Request, res: Response) => {
6-
const userId = res.locals.token.id;
7-
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
8-
res.json(await getAllOrganizationForUser(userId));
9-
};
7+
@Controller("admin")
8+
@ClassMiddleware(authHandler)
9+
export class AdminController {
10+
@Get("organizations")
11+
async getOrganizations(req: Request, res: Response) {
12+
const userId = res.locals.token.id;
13+
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
14+
res.json(await getAllOrganizationForUser(userId));
15+
}
1016

11-
export const routeAdminUsers = async (req: Request, res: Response) => {
12-
const userId = res.locals.token.id;
13-
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
14-
res.json(await getAllUsersForUser(userId));
15-
};
17+
@Get("users")
18+
async getUsers(req: Request, res: Response) {
19+
const userId = res.locals.token.id;
20+
if (!userId) throw new Error(ErrorCode.MISSING_FIELD);
21+
res.json(await getAllUsersForUser(userId));
22+
}
23+
}

src/routes/auth.ts

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,115 @@
11
import { Request, Response } from "express";
2-
import { ErrorCode, UserRole, Tokens } from "../interfaces/enum";
2+
import { ErrorCode } from "../interfaces/enum";
33
import {
44
sendPasswordReset,
55
login,
66
updatePassword,
7-
register,
87
validateRefreshToken,
98
loginWithGoogleLink,
109
loginWithGoogleVerify,
1110
impersonate,
12-
approveLocation
11+
approveLocation,
12+
verifyEmail
1313
} from "../rest/auth";
1414
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";
1523

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));
2632
}
27-
};
2833

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+
}
3548

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+
}
4257

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+
}
6065

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+
}
7075

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+
}
8282

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+
}
9190

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+
}
101100

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+
}
109109

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+
}

src/routes/emails.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)