11import { CookieOptions , Router } from "express" ;
22
3- import { isAuthorizedByEmail , isAuthorizedByUserId } from "../middlewares/auth" ;
3+ import {
4+ isAuthorizedByEmail ,
5+ isAuthorizedByUserId ,
6+ isAuthorizedByRole ,
7+ } from "../middlewares/auth" ;
48import {
59 loginRequestValidator ,
6- registerRequestValidator ,
10+ loginWithSignInLinkRequestValidator ,
11+ inviteUserDtoValidator ,
712} from "../middlewares/validators/authValidators" ;
813import nodemailerConfig from "../nodemailer.config" ;
914import AuthService from "../services/implementations/authService" ;
@@ -12,8 +17,8 @@ import UserService from "../services/implementations/userService";
1217import IAuthService from "../services/interfaces/authService" ;
1318import IEmailService from "../services/interfaces/emailService" ;
1419import IUserService from "../services/interfaces/userService" ;
15- import { getErrorMessage } from "../utilities/errorUtils" ;
16- import { Role } from "../types" ;
20+ import { getErrorMessage , NotFoundError } from "../utilities/errorUtils" ;
21+ import { UserStatus , Role } from "../types" ;
1722
1823const authRouter : Router = Router ( ) ;
1924const userService : IUserService = new UserService ( ) ;
@@ -45,36 +50,30 @@ authRouter.post("/login", loginRequestValidator, async (req, res) => {
4550 }
4651} ) ;
4752
48- /* Register a user, returns access token and user info in response body and sets refreshToken as an httpOnly cookie */
49- authRouter . post ( "/register" , registerRequestValidator , async ( req , res ) => {
50- try {
51- await userService . createUser ( {
52- firstName : req . body . firstName ,
53- lastName : req . body . lastName ,
54- email : req . body . email ,
55- role : req . body . role ?? Role . VOLUNTEER ,
56- skillLevel : req . body . skillLevel ?? null ,
57- canSeeAllLogs : req . body . canSeeAllLogs ?? null ,
58- canAssignUsersToTasks : req . body . canAssignUsersToTasks ?? null ,
59- phoneNumber : req . body . phoneNumber ?? null ,
60- } ) ;
61-
62- const authDTO = await authService . generateToken (
63- req . body . email ,
64- req . body . password ,
65- ) ;
66- const { refreshToken, ...rest } = authDTO ;
67-
68- await authService . sendEmailVerificationLink ( req . body . email ) ;
53+ /* Returns access token and user info in response body and sets refreshToken as an httpOnly cookie */
54+ authRouter . post (
55+ "/loginWithSignInLink" ,
56+ loginWithSignInLinkRequestValidator ,
57+ async ( req , res ) => {
58+ try {
59+ if ( isAuthorizedByEmail ( req . body . email ) ) {
60+ const userDTO = await userService . getUserByEmail ( req . body . email ) ;
61+ const rest = { ...{ accessToken : req . body . accessToken } , ...userDTO } ;
6962
70- res
71- . cookie ( "refreshToken" , refreshToken , cookieOptions )
72- . status ( 200 )
73- . json ( rest ) ;
74- } catch ( error : unknown ) {
75- res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
76- }
77- } ) ;
63+ res
64+ . cookie ( "refreshToken" , req . body . refreshToken , cookieOptions )
65+ . status ( 200 )
66+ . json ( rest ) ;
67+ }
68+ } catch ( error : unknown ) {
69+ if ( error instanceof NotFoundError ) {
70+ res . status ( 404 ) . send ( getErrorMessage ( error ) ) ;
71+ } else {
72+ res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
73+ }
74+ }
75+ } ,
76+ ) ;
7877
7978/* Returns access token in response body and sets refreshToken as an httpOnly cookie */
8079authRouter . post ( "/refresh" , async ( req , res ) => {
@@ -118,4 +117,45 @@ authRouter.post(
118117 } ,
119118) ;
120119
120+ /* Invite a user */
121+ authRouter . post ( "/invite-user" , inviteUserDtoValidator , async ( req , res ) => {
122+ try {
123+ if (
124+ ! isAuthorizedByRole (
125+ new Set ( [ Role . ADMINISTRATOR , Role . ANIMAL_BEHAVIOURIST ] ) ,
126+ )
127+ ) {
128+ res
129+ . status ( 401 )
130+ . json ( { error : "User is not authorized to invite user. " } ) ;
131+ return ;
132+ }
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+
140+ 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+
151+ res . status ( 204 ) . send ( ) ;
152+ } catch ( error : unknown ) {
153+ if ( error instanceof NotFoundError ) {
154+ res . status ( 404 ) . send ( getErrorMessage ( error ) ) ;
155+ } else {
156+ res . status ( 500 ) . json ( { error : getErrorMessage ( error ) } ) ;
157+ }
158+ }
159+ } ) ;
160+
121161export default authRouter ;
0 commit comments