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

Fix Google Auth displays Status: 401 on screen #7659

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

Pushpender1122
Copy link
Contributor

@Pushpender1122 Pushpender1122 commented Oct 13, 2024

When the user presses the cancel button, the server sends the following response:
image

{"statusCode": 401, "message": "Unauthorized"}

Now, when the user clicks the cancel button, they are redirected to the home page for login.

Related Issue
Fixes #7584

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

This PR modifies the exception handling in the server to redirect 401 Unauthorized errors to the front-end URL, addressing the issue of displaying a 401 status on screen during Google Auth cancellation.

  • Modified ApplyCorsToExceptions class in packages/twenty-server/src/utils/apply-cors-to-exceptions.ts to handle 401 Unauthorized exceptions
  • Added redirection logic for 401 errors to ${process.env.FRONT_BASE_URL}
  • Potential security risk introduced by altering exception handling for authentication errors
  • Assumes presence of FRONT_BASE_URL environment variable, which needs proper setting and validation
  • May affect API consistency and error handling for other scenarios using 401 status codes

1 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 35 to 38
if (status===401 && exception.message === 'Unauthorized') {
response.redirect(`${process.env.FRONT_BASE_URL}`);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: Redirecting on 401 may expose sensitive information. Consider returning a JSON response instead of redirecting.

@@ -32,7 +32,10 @@ export class ApplyCorsToExceptions implements ExceptionFilter {

const status =
exception instanceof HttpException ? exception.getStatus() : 500;

if (status===401 && exception.message === 'Unauthorized') {
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Use strict equality (===) for status comparison

@charlesBochet
Copy link
Member

@Bonapara what do you think? Maybe a snack bar in this case?

@charlesBochet
Copy link
Member

@Pushpender1122 Thanks for the PR, waiting for product's input

@Pushpender1122
Copy link
Contributor Author

Sure

@Bonapara
Copy link
Member

Maybe we should redirect to /welcome to allow the user to sign up with a different email. I believe selecting the wrong email is the most common scenario. WDYT? Did I get you well @charlesBochet?

@Pushpender1122
Copy link
Contributor Author

It redirects to /welcome. If you go to xyz.com, it automatically navigates to /welcome.

@Bonapara
Copy link
Member

Sounds like a good behavior, wondering what @charlesBochet had in mind.

@Pushpender1122
Copy link
Contributor Author

Hi @charlesBochet and @Bonapara , are there any other changes you'd like me to make?

@@ -33,6 +34,7 @@ export class GoogleAuthController {

@Get('redirect')
@UseGuards(GoogleProviderEnabledGuard, GoogleOauthGuard)
@UseFilters(AuthOAuthExceptionFilter)
Copy link
Member

Choose a reason for hiding this comment

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

using an adhoc filter to redirect instead of the ApplyCorsToExceptions globalFilter which is not meant to do that

if (request.query.error === 'access_denied') {
throw new AuthException(
'Google OAuth access denied',
AuthExceptionCode.OAUTH_ACCESS_DENIED,
Copy link
Member

Choose a reason for hiding this comment

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

throwing a precise Auth Exception exception in the guard

Copy link
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

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

Thanks @Pushpender1122. I have made a few changes that are more in the "nestjs" philosophy

@charlesBochet
Copy link
Member

/award 150

Copy link

oss-gg bot commented Oct 17, 2024

Awarding Pushpender1122: 150 points 🕹️ Well done! Check out your new contribution on oss.gg/Pushpender1122

@charlesBochet charlesBochet merged commit 8f7ca6a into twentyhq:main Oct 17, 2024
4 checks passed
Copy link

oss-gg bot commented Oct 17, 2024

Awarding Pushpender1122: 150 points 🕹️ Well done! Check out your new contribution on oss.gg/Pushpender1122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The failure of Google Auth displays "Status: 401" on screen
3 participants