From b1909337dbd9c794e43d8c8a3e3785d5a12cce82 Mon Sep 17 00:00:00 2001 From: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Date: Fri, 10 May 2024 19:09:45 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20SAML=20fixes=20for=20uppercase=20email?= =?UTF-8?q?=20&=20GOOGLE=20=E2=86=92=20SAML=20idp=20switch=20(#14971)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix uppercase email slip * fix google → SAML idp change --- .../features/auth/lib/next-auth-options.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts index 5f391a66df4641..a0bf241de0f567 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";