Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 25 additions & 58 deletions packages/server/src/api_auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Copy link
Owner

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 .

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to validate , already validated : res.locals.reqClientData. Just use properties directly from res.locals.reqClientData

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');
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The 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,
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}
});

Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The 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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;

}
});
Expand Down