From 1ee3783db30fe62dcf7fe1eef746759275525ecc Mon Sep 17 00:00:00 2001 From: Deepak Prabhakara Date: Wed, 8 Feb 2023 18:39:56 +0000 Subject: [PATCH] Allow account linking for Google and SAML providers (#6874) * allow account linking for self-hosted instances, both Google and SAML are verified emails * allow account linking for Google and SSO if emails match with existing username/password account * Tweaked find user by email since we now have multiple providers (other than credentials provider) --- apps/web/pages/api/auth/[...nextauth].tsx | 10 +++++++++- apps/web/pages/api/auth/signup.ts | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/apps/web/pages/api/auth/[...nextauth].tsx b/apps/web/pages/api/auth/[...nextauth].tsx index ce59414763..54976e8202 100644 --- a/apps/web/pages/api/auth/[...nextauth].tsx +++ b/apps/web/pages/api/auth/[...nextauth].tsx @@ -164,6 +164,7 @@ if (IS_GOOGLE_LOGIN_ENABLED) { GoogleProvider({ clientId: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_CLIENT_SECRET, + allowDangerousEmailAccountLinking: true, }) ); } @@ -202,6 +203,7 @@ if (isSAMLLoginEnabled) { clientId: "dummy", clientSecret: "dummy", }, + allowDangerousEmailAccountLinking: true, }); } @@ -497,7 +499,13 @@ export default NextAuth({ return true; } - if (existingUserWithEmail.identityProvider === IdentityProvider.CAL) { + // User signs up with email/password and then tries to login with Google/SAML using the same email + if ( + existingUserWithEmail.identityProvider === IdentityProvider.CAL && + (idP === IdentityProvider.GOOGLE || idP === IdentityProvider.SAML) + ) { + return true; + } else if (existingUserWithEmail.identityProvider === IdentityProvider.CAL) { return "/auth/error?error=use-password-login"; } diff --git a/apps/web/pages/api/auth/signup.ts b/apps/web/pages/api/auth/signup.ts index 61b540ec0a..a838f0d47d 100644 --- a/apps/web/pages/api/auth/signup.ts +++ b/apps/web/pages/api/auth/signup.ts @@ -43,7 +43,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) OR: [ { username }, { - AND: [{ email: userEmail }, { password: { not: null } }, { username: { not: null } }], + AND: [{ email: userEmail }], }, ], },