From 6255cead3760e855033dddb55d780b7bd5c6f88a Mon Sep 17 00:00:00 2001 From: sabbellasri Date: Fri, 1 Dec 2023 23:43:04 -0500 Subject: [PATCH 1/3] Refactoring /Google Route --- packages/server/src/api_auth/index.ts | 84 +++++++++------------------ 1 file changed, 26 insertions(+), 58 deletions(-) diff --git a/packages/server/src/api_auth/index.ts b/packages/server/src/api_auth/index.ts index e9ca91c..a78b3f2 100644 --- a/packages/server/src/api_auth/index.ts +++ b/packages/server/src/api_auth/index.ts @@ -7,7 +7,8 @@ import rateLimit from 'express-rate-limit'; import { v4 } from 'uuid'; import { ZodError } from 'zod'; import { Constants, generateToken, isMagicTokenValid, jwtExpireDate, PrismaClientSingleton, verifyGoogleAuthToken } from '../utils'; -import { emailPasswordObjectValidator, tokenEmailObjectValidator, tokenObjectValidator } from '../validators'; +//tslint:disable-next-line: no-unused-variable +import { emailPasswordObjectValidator,tokenEmailObjectValidator, tokenObjectValidator } from '../validators'; import { apiRequestAuthGoogleLoginValidator, apiRequestAuthGoogleValidator, @@ -463,22 +464,27 @@ router.post('/magic_login', apiRequestAuthMagicLoginValidator, * Signup with google * POSTMAN_TODO : This route is waiting to be added to postman and documented */ -router.post('/google', apiRequestAuthGoogleValidator, async (req, res) => { +// Helper function for sending error responses +const sendGErrorResponse = (res: any,status:number,error:string) => { + const response:ApiResponse = { + success: false, + status:status, + error:error, + }; + res.status(status).send(response); +}; +router.post('/google', apiRequestAuthGoogleValidator, async (req,res) => { + // token is required try { // Validate the request body using the Zod schema - const parsedBody = await tokenObjectValidator.parseAsync(req.body); + const parsedBody = await tokenObjectValidator.parseAsync(res.locals.reqClientData); const token = parsedBody.token; const tokenPayload = await verifyGoogleAuthToken(token); if (!tokenPayload.success) { - const response:ApiResponse = { - success : false , - status : 401, - error:'Invalid token' - } - res.status(401).send(response); + sendGErrorResponse(res, 401, 'Invalid token'); return; } @@ -492,12 +498,7 @@ router.post('/google', apiRequestAuthGoogleValidator, async (req, res) => { }); if (oldUser) { - const response:ApiResponse = { - success : false , - status : 401, - error:'User with this email already exists' - } - res.status(401).send(response); + sendGErrorResponse(res, 401, 'User with this email already exists'); return; } @@ -529,20 +530,11 @@ router.post('/google', apiRequestAuthGoogleValidator, async (req, res) => { return; } catch (error) { if (error instanceof ZodError && !error.isEmpty) { - const response:ApiResponse = { - success : false , - status : 400, - error:'Token is required and must be non-empty' - } - res.status(400).send(response); + sendGErrorResponse(res, 400, 'Token is required and must be non-empty'); return; } - const response:ApiResponse = { - success : false , - status : 400, - error:error - } - return res.status(400).send(response); + sendGErrorResponse(res, 400, "An unexpected error occurred "+error); + return; } }); @@ -555,17 +547,12 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res // token is required try { // Validate the request body using the Zod schema - const parsedBody = await tokenObjectValidator.parseAsync(req.body); + const parsedBody = await tokenObjectValidator.parseAsync(res.locals.reqClientData); const token = parsedBody.token; const tokenPayload = await verifyGoogleAuthToken(token); if (!tokenPayload.success) { - const response:ApiResponse = { - success : false , - status : 401, - error:'Invalid token' - } - res.status(401).send(response); + sendGErrorResponse(res, 401, 'Invalid token'); return; } @@ -583,12 +570,7 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res }); if (!oldUser) { - const response:ApiResponse = { - success : false , - status : 401, - error:'Invalid token' - } - res.status(401).send(response); + sendGErrorResponse(res, 401, 'User must be registered to sign in'); return; } @@ -597,12 +579,7 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res // check for the number of active sessions if (oldUser.numberOfSessions === oldUser.sessions.length) { - const response:ApiResponse = { - success : false , - status : 401, - error:'Too many sessions' - } - res.status(401).send(response); + sendGErrorResponse(res, 401, 'Too many Sessions'); return; } @@ -638,20 +615,11 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res return; } catch (error) { if (error instanceof ZodError && !error.isEmpty) { - const response:ApiResponse = { - success : false , - status : 400, - error:'Token is required and must be non-empty' - } - res.status(400).send(response); + sendGErrorResponse(res, 400, 'Token is required and must be non-empty'); return; } - const response:ApiResponse = { - success : false , - status : 400, - error:error - } - return res.status(400).send(response); + sendGErrorResponse(res, 400, "An unexpected error occurred "+error); + return; } }); From c05628debe6617e29fab3d4daa851ba5750a3c45 Mon Sep 17 00:00:00 2001 From: sabbellasri Date: Fri, 1 Dec 2023 23:47:18 -0500 Subject: [PATCH 2/3] Refactoring /Google Route --- packages/server/src/api_auth/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/server/src/api_auth/index.ts b/packages/server/src/api_auth/index.ts index a78b3f2..5a201c3 100644 --- a/packages/server/src/api_auth/index.ts +++ b/packages/server/src/api_auth/index.ts @@ -7,7 +7,6 @@ import rateLimit from 'express-rate-limit'; import { v4 } from 'uuid'; import { ZodError } from 'zod'; import { Constants, generateToken, isMagicTokenValid, jwtExpireDate, PrismaClientSingleton, verifyGoogleAuthToken } from '../utils'; -//tslint:disable-next-line: no-unused-variable import { emailPasswordObjectValidator,tokenEmailObjectValidator, tokenObjectValidator } from '../validators'; import { apiRequestAuthGoogleLoginValidator, From b30a3e240344761d147d514e9f7c26255b536027 Mon Sep 17 00:00:00 2001 From: sabbellasri Date: Mon, 4 Dec 2023 16:06:22 -0500 Subject: [PATCH 3/3] Refactoring google route --- packages/server/src/api_auth/index.ts | 85 +++++++++++++++++++-------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/packages/server/src/api_auth/index.ts b/packages/server/src/api_auth/index.ts index 5a201c3..3a52e97 100644 --- a/packages/server/src/api_auth/index.ts +++ b/packages/server/src/api_auth/index.ts @@ -7,7 +7,7 @@ import rateLimit from 'express-rate-limit'; import { v4 } from 'uuid'; import { ZodError } from 'zod'; import { Constants, generateToken, isMagicTokenValid, jwtExpireDate, PrismaClientSingleton, verifyGoogleAuthToken } from '../utils'; -import { emailPasswordObjectValidator,tokenEmailObjectValidator, tokenObjectValidator } from '../validators'; +import { emailPasswordObjectValidator, tokenEmailObjectValidator, tokenObjectValidator } from '../validators'; import { apiRequestAuthGoogleLoginValidator, apiRequestAuthGoogleValidator, @@ -463,27 +463,22 @@ router.post('/magic_login', apiRequestAuthMagicLoginValidator, * Signup with google * POSTMAN_TODO : This route is waiting to be added to postman and documented */ -// Helper function for sending error responses -const sendGErrorResponse = (res: any,status:number,error:string) => { - const response:ApiResponse = { - success: false, - status:status, - error:error, - }; - res.status(status).send(response); -}; -router.post('/google', apiRequestAuthGoogleValidator, async (req,res) => { - +router.post('/google', apiRequestAuthGoogleValidator, async (req, res) => { // token is required try { // Validate the request body using the Zod schema - const parsedBody = await tokenObjectValidator.parseAsync(res.locals.reqClientData); + const parsedBody = res.locals.reqClientData; const token = parsedBody.token; const tokenPayload = await verifyGoogleAuthToken(token); if (!tokenPayload.success) { - sendGErrorResponse(res, 401, 'Invalid token'); + const response:ApiResponse = { + success : false , + status : 401, + error:'Invalid token' + } + res.status(401).send(response); return; } @@ -497,7 +492,12 @@ router.post('/google', apiRequestAuthGoogleValidator, async (req,res) => { }); if (oldUser) { - sendGErrorResponse(res, 401, 'User with this email already exists'); + const response:ApiResponse = { + success : false , + status : 401, + error:'User with this email already exists' + } + res.status(401).send(response); return; } @@ -529,11 +529,20 @@ router.post('/google', apiRequestAuthGoogleValidator, async (req,res) => { return; } catch (error) { if (error instanceof ZodError && !error.isEmpty) { - sendGErrorResponse(res, 400, 'Token is required and must be non-empty'); + const response:ApiResponse = { + success : false , + status : 400, + error: error.issues[0]?.message + } + res.status(400).send(response); return; } - sendGErrorResponse(res, 400, "An unexpected error occurred "+error); - return; + const response:ApiResponse = { + success : false , + status : 500, + error:error + } + return res.status(500).send(response); } }); @@ -546,12 +555,17 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res // token is required try { // Validate the request body using the Zod schema - const parsedBody = await tokenObjectValidator.parseAsync(res.locals.reqClientData); + const parsedBody = res.locals.reqClientData; const token = parsedBody.token; const tokenPayload = await verifyGoogleAuthToken(token); if (!tokenPayload.success) { - sendGErrorResponse(res, 401, 'Invalid token'); + const response:ApiResponse = { + success : false , + status : 401, + error:'Invalid token' + } + res.status(401).send(response); return; } @@ -569,7 +583,12 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res }); if (!oldUser) { - sendGErrorResponse(res, 401, 'User must be registered to sign in'); + const response:ApiResponse = { + success : false , + status : 401, + error:'Invalid token' + } + res.status(401).send(response); return; } @@ -578,7 +597,12 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res // check for the number of active sessions if (oldUser.numberOfSessions === oldUser.sessions.length) { - sendGErrorResponse(res, 401, 'Too many Sessions'); + const response:ApiResponse = { + success : false , + status : 401, + error:'Too many sessions' + } + res.status(401).send(response); return; } @@ -614,11 +638,20 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res return; } catch (error) { if (error instanceof ZodError && !error.isEmpty) { - sendGErrorResponse(res, 400, 'Token is required and must be non-empty'); + const response:ApiResponse = { + success : false , + status : 400, + error: error.issues[0]?.message + } + res.status(400).send(response); return; } - sendGErrorResponse(res, 400, "An unexpected error occurred "+error); - return; + const response:ApiResponse = { + success : false , + status : 500, + error:error + } + return res.status(500).send(response); } }); @@ -779,4 +812,4 @@ router.post('/logout_all', apiRequestAuthLogoutAllValidator, async (_req, res) = } }); -export default router; +export default router; \ No newline at end of file