|
| 1 | +import nodemailerConfig from '../../nodemailer.config'; |
| 2 | +import AuthService from '../../services/implementations/authService'; |
| 3 | +import EmailService from '../../services/implementations/emailService'; |
| 4 | +import UserService from '../../services/implementations/userService'; |
| 5 | +import IAuthService from '../../services/interfaces/authService'; |
| 6 | +import IEmailService from '../../services/interfaces/emailService'; |
| 7 | +import IUserService from '../../services/interfaces/userService'; |
| 8 | +import { AuthDTO, RegisterUserDTO, Role, SignUpMethod } from '../../types'; |
| 9 | + |
| 10 | +const userService: IUserService = new UserService(); |
| 11 | +const emailService: IEmailService = new EmailService(nodemailerConfig); |
| 12 | +const authService: IAuthService = new AuthService(userService, emailService); |
| 13 | + |
| 14 | +const authResolvers = { |
| 15 | + Query: { |
| 16 | + isAuthorizedByRole: async ( |
| 17 | + _parent: undefined, |
| 18 | + { accessToken, roles }: { accessToken: string; roles: Role[] } |
| 19 | + ): Promise<boolean> => { |
| 20 | + const isAuthorized = await authService.isAuthorizedByRole(accessToken, new Set(roles)); |
| 21 | + return isAuthorized; |
| 22 | + }, |
| 23 | + isAuthorizedByUserId: async ( |
| 24 | + _parent: undefined, |
| 25 | + { accessToken, userId }: { accessToken: string; userId: string } |
| 26 | + ): Promise<boolean> => { |
| 27 | + const isAuthorized = await authService.isAuthorizedByUserId(accessToken, userId); |
| 28 | + return isAuthorized; |
| 29 | + }, |
| 30 | + isAuthorizedByEmail: async ( |
| 31 | + _parent: undefined, |
| 32 | + { accessToken, email }: { accessToken: string; email: string } |
| 33 | + ): Promise<boolean> => { |
| 34 | + const isAuthorized = await authService.isAuthorizedByEmail(accessToken, email); |
| 35 | + return isAuthorized; |
| 36 | + }, |
| 37 | + }, |
| 38 | + Mutation: { |
| 39 | + login: async ( |
| 40 | + _parent: undefined, |
| 41 | + { email, password }: { email: string; password: string } |
| 42 | + ): Promise<AuthDTO> => { |
| 43 | + const authDTO = await authService.generateToken(email, password); |
| 44 | + return authDTO; |
| 45 | + }, |
| 46 | + loginWithGoogle: async ( |
| 47 | + _parent: undefined, |
| 48 | + { idToken }: { idToken: string } |
| 49 | + ): Promise<AuthDTO> => { |
| 50 | + const authDTO = await authService.generateTokenOAuth(idToken); |
| 51 | + return authDTO; |
| 52 | + }, |
| 53 | + register: async (_parent: undefined, { user }: { user: RegisterUserDTO }): Promise<AuthDTO> => { |
| 54 | + if (!user.password) { |
| 55 | + throw new Error('Password is required for registration'); |
| 56 | + } |
| 57 | + const newUser = await userService.createUser( |
| 58 | + { |
| 59 | + ...user, |
| 60 | + role: Role.User, // Default role for registration |
| 61 | + }, |
| 62 | + undefined, |
| 63 | + SignUpMethod.PASSWORD |
| 64 | + ); |
| 65 | + await authService.sendEmailVerificationLink(newUser.email); |
| 66 | + const authDTO = await authService.generateToken(user.email, user.password); |
| 67 | + return authDTO; |
| 68 | + }, |
| 69 | + refresh: async ( |
| 70 | + _parent: undefined, |
| 71 | + { refreshToken }: { refreshToken: string } |
| 72 | + ): Promise<string> => { |
| 73 | + const token = await authService.renewToken(refreshToken); |
| 74 | + return token.accessToken; |
| 75 | + }, |
| 76 | + logout: async (_parent: undefined, { userId }: { userId: string }): Promise<boolean> => { |
| 77 | + await authService.revokeTokens(userId); |
| 78 | + return true; |
| 79 | + }, |
| 80 | + resetPassword: async (_parent: undefined, { email }: { email: string }): Promise<boolean> => { |
| 81 | + await authService.resetPassword(email); |
| 82 | + return true; |
| 83 | + }, |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +export default authResolvers; |
0 commit comments