Skip to content

Conversation

@michaelcanova
Copy link
Contributor

Issue

One large issue we noticed, was that if a user tried to log in using Google's SSO when they already have a base email/password account, it would fail and lead to a blank page. A blank page can be confusing to the user, so the solution would be to redirect them to our individual login page. We ran into registration issues in the past as well with this fix, which is now resolved after thorough testing.

Before

Screenshot 2025-10-23 110455

After

Screenshot 2025-10-23 110431

@michaelcanova michaelcanova requested a review from a team as a code owner October 23, 2025 16:29
@vercel
Copy link

vercel bot commented Oct 23, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
web Ready Ready Preview Comment Oct 23, 2025 8:14pm

Copy link

@syqs syqs left a comment

Choose a reason for hiding this comment

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

Overall good improvement just nitpicking around consistency.

// Debug flag tho, feels like an oversight that should be remedied before going to prod
const DEBUG_AUTH = true;

const emailInputRef = useAutoFocus<HTMLInputElement>(true);
const { referralCode } = useReferral();

const getCallbackUrl = () => {
Copy link

Choose a reason for hiding this comment

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

Good refactor 🦾

Copy link
Contributor Author

Choose a reason for hiding this comment

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

❤️

return true;
} catch (error) {
// Log detailed error information
const errorType = error instanceof Error ? error.message : 'AuthenticationFailed';
Copy link

Choose a reason for hiding this comment

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

Should standardize error handling and use consistent error classes (:144-150)
Good usage example in auth.service.ts and services/publication.service.ts (Lines 52-54, 69-76):

// Should be something like this:
const errorType = error instanceof Error ? error.message : 'AuthenticationFailed';
console.error('[Auth] Google OAuth error', {
error: errorType,
errorType: error instanceof Error ? error.constructor.name : typeof error,
stack: error instanceof Error ? error.stack : undefined,
timestamp: new Date().toISOString(),
});
throw new NextAuthError(errorType, 'OAUTH_ERROR');

// add this to the top after imports
export class NextAuthError extends Error {
constructor(
message: string,
public readonly code: string
) {
super(message);
this.name = 'NextAuthError';
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I agree!

throw new AuthError(errorMsg || 'Registration failed', status);
if (error instanceof ApiError) {
const data = error.errors || {};
const errorMsg = Object.values(data as Record<string, string[]>)?.[0]?.[0];
Copy link

Choose a reason for hiding this comment

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

This could perhaps be refactored for slightly better readability and consistency:

const errorValues = Object.values(data as Record<string, string[]>)?.[0];
const errorMsg = Array.isArray(errorValues)
? errorValues[0]
: typeof errorValues === 'string'
? errorValues
: 'Registration failed';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me, ill update and test it now!

import { AuthSharingService } from '@/services/auth-sharing.service';

// Debug flag - set to true to enable detailed authentication logging
const DEBUG_AUTH = true;
Copy link

Choose a reason for hiding this comment

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

I see this change has been here a while but it feels like security risk so maybe we should change it to something like:

const DEBUG_AUTH = process.env.NODE_ENV === 'development';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll defer to @yattias on whether this is something he wants changed currently.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove it in another PR

@sonarqubecloud
Copy link

Copy link
Contributor

@nicktytarenko nicktytarenko left a comment

Choose a reason for hiding this comment

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

i think we can simplify the logic a bit. i am not 100% sure but according to the nextjs doc it can be simplified a bit

},
callbacks: {
async signIn({ user, account, profile }) {
async signIn({ user, account, profile, email, credentials }) {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need new props?

Comment on lines +27 to +32
errorCode === 'OAuthAccountNotLinked' ||
errorCode === 'OAuthSignin' ||
errorCode === 'OAuthCallback' ||
errorCode === 'AccessDenied' ||
errorCode === 'Callback' ||
errorCode === 'OAuthCreateAccount' ||
Copy link
Contributor

Choose a reason for hiding this comment

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

where are these errors specified?

Comment on lines +185 to 187
if (response.status === 400 || response.status === 403 || response.status === 409) {
throw new Error('OAuthAccountNotLinked');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we are doing wrong mapping here. because the error we are trying to handle is resulting with 400 error (When trying to register with an email that's already registered).

// Return false for OAuthAccountNotLinked to trigger consistent error flow
// This ensures the error is properly handled by the redirect callback
if (errorType === 'OAuthAccountNotLinked') {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the NextAuth documentation, the signIn callback can return a URL string to redirect users directly. This would eliminate the current redirect chain and provide a cleaner user experience.

Current approach:

  • Return false → NextAuth redirects to /auth/error
  • Error page redirects to /auth/signin with error param
  • Sign-in page processes error code and shows message

Proposed approach:

  • Return URL directly: /auth/signin?errorMessage=Please use email and password to login to your account.
  • Sign-in page reads errorMessage param and displays it
  • Remove errorMessage param from URL after rendering to clean up the URL

Benefits:

  • Eliminates unnecessary redirect chain
  • Cleaner, more maintainable code
  • Custom error messages without complex error code mapping

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants