diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts index 5f391a66df464..a0bf241de0f56 100644 --- a/packages/features/auth/lib/next-auth-options.ts +++ b/packages/features/auth/lib/next-auth-options.ts @@ -342,7 +342,8 @@ if (isSAMLLoginEnabled) { return null; } - const { id, firstName, lastName, email } = userInfo; + const { id, firstName, lastName } = userInfo; + const email = userInfo.email.toLowerCase(); let user = !email ? undefined : await UserRepository.findByEmailAndIncludeProfilesAndPassword({ email }); @@ -844,7 +845,7 @@ export const AUTH_OPTIONS: AuthOptions = { where: { email: existingUserWithEmail.email }, // also update email to the IdP email data: { - email: user.email, + email: user.email.toLowerCase(), identityProvider: idP, identityProviderId: account.providerAccountId, }, @@ -857,6 +858,19 @@ export const AUTH_OPTIONS: AuthOptions = { } } else if (existingUserWithEmail.identityProvider === IdentityProvider.CAL) { return "/auth/error?error=use-password-login"; + } else if ( + existingUserWithEmail.identityProvider === IdentityProvider.GOOGLE && + idP === IdentityProvider.SAML + ) { + await prisma.user.update({ + where: { email: existingUserWithEmail.email }, + // also update email to the IdP email + data: { + email: user.email.toLowerCase(), + identityProvider: idP, + identityProviderId: account.providerAccountId, + }, + }); } return "/auth/error?error=use-identity-login";