From 2685e8154f0640606f987581f27e5dc57ce10450 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Thu, 16 Mar 2023 15:12:45 +0000 Subject: [PATCH] Hotfix/account provider should be lowercase (#7763) * Fix passThrough console error (passHref is not valid) * Fix 'use-identity-login' error when logging in with idP --------- Co-authored-by: Keith Williams --- apps/web/pages/auth/logout.tsx | 2 +- .../features/auth/lib/next-auth-options.ts | 39 ++++++++++++++++--- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/apps/web/pages/auth/logout.tsx b/apps/web/pages/auth/logout.tsx index 686546fd13..be7d25ed1a 100644 --- a/apps/web/pages/auth/logout.tsx +++ b/apps/web/pages/auth/logout.tsx @@ -43,7 +43,7 @@ export default function Logout(props: Props) { - diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts index ea34e9c906..042d29b8c9 100644 --- a/packages/features/auth/lib/next-auth-options.ts +++ b/packages/features/auth/lib/next-auth-options.ts @@ -444,7 +444,7 @@ export const AUTH_OPTIONS: AuthOptions = { identityProvider: idP, }, { - identityProviderId: account.providerAccountId as string, + identityProviderId: account.providerAccountId, }, ], }, @@ -523,11 +523,11 @@ export const AUTH_OPTIONS: AuthOptions = { return "/auth/error?error=unverified-email"; } - const existingUser = await prisma.user.findFirst({ + let existingUser = await prisma.user.findFirst({ include: { accounts: { where: { - provider: idP, + provider: account.provider, }, }, }, @@ -537,6 +537,33 @@ export const AUTH_OPTIONS: AuthOptions = { }, }); + /* --- START FIX LEGACY ISSUE WHERE 'identityProviderId' was accidentally set to userId --- */ + if (!existingUser) { + existingUser = await prisma.user.findFirst({ + include: { + accounts: { + where: { + provider: account.provider, + }, + }, + }, + where: { + identityProvider: idP, + identityProviderId: String(user.id), + }, + }); + if (existingUser) { + await prisma.user.update({ + where: { + id: existingUser?.id, + }, + data: { + identityProviderId: account.providerAccountId, + }, + }); + } + } + /* --- END FIXES LEGACY ISSUE WHERE 'identityProviderId' was accidentally set to userId --- */ if (existingUser) { // In this case there's an existing user and their email address // hasn't changed since they last logged in. @@ -617,7 +644,7 @@ export const AUTH_OPTIONS: AuthOptions = { emailVerified: new Date(Date.now()), name: user.name, identityProvider: idP, - identityProviderId: String(user.id), + identityProviderId: account.providerAccountId, }, }); @@ -640,7 +667,7 @@ export const AUTH_OPTIONS: AuthOptions = { password: null, email: user.email, identityProvider: idP, - identityProviderId: String(user.id), + identityProviderId: account.providerAccountId, }, }); if (existingUserWithEmail.twoFactorEnabled) { @@ -664,7 +691,7 @@ export const AUTH_OPTIONS: AuthOptions = { name: user.name, email: user.email, identityProvider: idP, - identityProviderId: String(user.id), + identityProviderId: account.providerAccountId, }, });