-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactoring google route #239
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,22 +463,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<null> = { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to validate , already validated : |
||
const token = parsedBody.token; | ||
|
||
const tokenPayload = await verifyGoogleAuthToken(token); | ||
|
||
if (!tokenPayload.success) { | ||
const response:ApiResponse<null> = { | ||
success : false , | ||
status : 401, | ||
error:'Invalid token' | ||
} | ||
res.status(401).send(response); | ||
sendGErrorResponse(res, 401, 'Invalid token'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to use helper function put that in utils.ts file in server folder. |
||
return; | ||
} | ||
|
||
|
@@ -492,12 +497,7 @@ router.post('/google', apiRequestAuthGoogleValidator, async (req, res) => { | |
}); | ||
|
||
if (oldUser) { | ||
const response:ApiResponse<null> = { | ||
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 +529,11 @@ router.post('/google', apiRequestAuthGoogleValidator, async (req, res) => { | |
return; | ||
} catch (error) { | ||
if (error instanceof ZodError && !error.isEmpty) { | ||
const response:ApiResponse<null> = { | ||
success : false , | ||
status : 400, | ||
error:'Token is required and must be non-empty' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use error message from ZOD |
||
} | ||
res.status(400).send(response); | ||
sendGErrorResponse(res, 400, 'Token is required and must be non-empty'); | ||
return; | ||
} | ||
const response:ApiResponse<null> = { | ||
success : false , | ||
status : 400, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Status code should be 500 . Internal server error. Not 400. |
||
error:error | ||
} | ||
return res.status(400).send(response); | ||
sendGErrorResponse(res, 400, "An unexpected error occurred "+error); | ||
return; | ||
} | ||
}); | ||
|
||
|
@@ -555,17 +546,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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is issue related to this route . Submit Your PR with that issue. |
||
const token = parsedBody.token; | ||
const tokenPayload = await verifyGoogleAuthToken(token); | ||
|
||
if (!tokenPayload.success) { | ||
const response:ApiResponse<null> = { | ||
success : false , | ||
status : 401, | ||
error:'Invalid token' | ||
} | ||
res.status(401).send(response); | ||
sendGErrorResponse(res, 401, 'Invalid token'); | ||
return; | ||
} | ||
|
||
|
@@ -583,12 +569,7 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res | |
}); | ||
|
||
if (!oldUser) { | ||
const response:ApiResponse<null> = { | ||
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 +578,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<null> = { | ||
success : false , | ||
status : 401, | ||
error:'Too many sessions' | ||
} | ||
res.status(401).send(response); | ||
sendGErrorResponse(res, 401, 'Too many Sessions'); | ||
return; | ||
} | ||
|
||
|
@@ -638,20 +614,11 @@ router.post('/google_login', apiRequestAuthGoogleLoginValidator, async (req, res | |
return; | ||
} catch (error) { | ||
if (error instanceof ZodError && !error.isEmpty) { | ||
const response:ApiResponse<null> = { | ||
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<null> = { | ||
success : false , | ||
status : 400, | ||
error:error | ||
} | ||
return res.status(400).send(response); | ||
sendGErrorResponse(res, 400, "An unexpected error occurred "+error); | ||
return; | ||
|
||
} | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not write global helper function here . Put that in utils.ts file in server folder . But i request you not to use helper function for this .