Skip to content

Commit a224aa0

Browse files
committed
Change authorization for invite-user
1 parent 069a80c commit a224aa0

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

backend/typescript/rest/authRoutes.ts

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { CookieOptions, Router } from "express";
22

3-
import { isAuthorizedByEmail, isAuthorizedByUserId } from "../middlewares/auth";
3+
import {
4+
isAuthorizedByEmail,
5+
isAuthorizedByUserId,
6+
isAuthorizedByRole,
7+
} from "../middlewares/auth";
48
import {
59
loginRequestValidator,
610
loginWithSignInLinkRequestValidator,
@@ -14,7 +18,7 @@ import IAuthService from "../services/interfaces/authService";
1418
import IEmailService from "../services/interfaces/emailService";
1519
import IUserService from "../services/interfaces/userService";
1620
import { getErrorMessage, NotFoundError } from "../utilities/errorUtils";
17-
import { UserStatus } from "../types";
21+
import { UserStatus, Role } from "../types";
1822

1923
const authRouter: Router = Router();
2024
const userService: IUserService = new UserService();
@@ -71,37 +75,6 @@ authRouter.post(
7175
},
7276
);
7377

74-
/* Register a user, returns access token and user info in response body and sets refreshToken as an httpOnly cookie */
75-
/* authRouter.post("/register", registerRequestValidator, async (req, res) => {
76-
try {
77-
await userService.createUser({
78-
firstName: req.body.firstName,
79-
lastName: req.body.lastName,
80-
email: req.body.email,
81-
role: req.body.role ?? Role.VOLUNTEER,
82-
skillLevel: req.body.skillLevel ?? null,
83-
canSeeAllLogs: req.body.canSeeAllLogs ?? null,
84-
canAssignUsersToTasks: req.body.canAssignUsersToTasks ?? null,
85-
phoneNumber: req.body.phoneNumber ?? null,
86-
});
87-
88-
const authDTO = await authService.generateToken(
89-
req.body.email,
90-
req.body.password,
91-
);
92-
const { refreshToken, ...rest } = authDTO;
93-
94-
await authService.sendEmailVerificationLink(req.body.email);
95-
96-
res
97-
.cookie("refreshToken", refreshToken, cookieOptions)
98-
.status(200)
99-
.json(rest);
100-
} catch (error: unknown) {
101-
res.status(500).json({ error: getErrorMessage(error) });
102-
}
103-
}); */
104-
10578
/* Returns access token in response body and sets refreshToken as an httpOnly cookie */
10679
authRouter.post("/refresh", async (req, res) => {
10780
try {
@@ -147,15 +120,34 @@ authRouter.post(
147120
/* Invite a user */
148121
authRouter.post("/invite-user", inviteUserDtoValidator, async (req, res) => {
149122
try {
150-
const user = await userService.getUserByEmail(req.body.email);
151123
if (
152-
user.status === UserStatus.ACTIVE ||
153-
user.status === UserStatus.INACTIVE
124+
!isAuthorizedByRole(
125+
new Set([Role.ADMINISTRATOR, Role.ANIMAL_BEHAVIOURIST]),
126+
)
154127
) {
155-
res.status(400).json({ error: "User has already been invited." });
128+
res
129+
.status(400)
130+
.json({ error: "User is not authorized to invite user. " });
156131
return;
157132
}
133+
134+
const user = await userService.getUserByEmail(req.body.email);
135+
if (user.status === UserStatus.ACTIVE) {
136+
res.status(400).json({ error: "User has already claimed account." });
137+
return;
138+
}
139+
158140
await authService.sendInviteEmail(req.body.email, String(user.role));
141+
if (user.status === UserStatus.INVITED) {
142+
res
143+
.status(204)
144+
.send("Success. Previous invitation has been invalidated.");
145+
return;
146+
}
147+
const invitedUser = user;
148+
invitedUser.status = UserStatus.INVITED;
149+
await userService.updateUserById(user.id, invitedUser);
150+
159151
res.status(204).send();
160152
} catch (error: unknown) {
161153
if (error instanceof NotFoundError) {

0 commit comments

Comments
 (0)