diff --git a/app/api/auth/[...nextauth]/route.ts b/app/api/auth/[...nextauth]/route.ts index e9eabbb..5d989a6 100644 --- a/app/api/auth/[...nextauth]/route.ts +++ b/app/api/auth/[...nextauth]/route.ts @@ -18,45 +18,44 @@ const authOptions: NextAuthOptions = { ], adapter: FirestoreAdapter(adminDb), callbacks: { - async signIn({ user, account, profile }) { - try { - const userDocRef = adminDb.collection("users").doc(user.email!); - const userDoc = await userDocRef.get(); + async signIn({ user, account, profile }) { + try { + const userDocRef = adminDb.collection("users").doc(user.email!); + const userDoc = await userDocRef.get(); - if (userDoc.exists) { - const userData = userDoc.data(); + if (userDoc.exists) { + const userData = userDoc.data(); - if ( - userData.provider && - userData.provider !== account.provider && - !userData.linkedProviders?.includes(account.provider) - ) { - throw new Error("OAuthAccountNotLinked"); - } + if ( + userData.provider && + userData.provider !== account.provider && + !userData.linkedProviders?.includes(account.provider) + ) { + throw new Error("OAuthAccountNotLinked"); + } + + if (!userData.linkedProviders?.includes(account.provider)) { + await userDocRef.update({ + linkedProviders: [...(userData.linkedProviders || []), account.provider], + }); + } - if (!userData.linkedProviders?.includes(account.provider)) { - await userDocRef.update({ - linkedProviders: [...(userData.linkedProviders || []), account.provider], + return true; + } else { + await userDocRef.set({ + email: user.email!, + plan: "free", + requestCount: 0, + provider: account.provider, + linkedProviders: [account.provider], }); + return true; } - - return true; - } else { - await userDocRef.set({ - email: user.email!, - plan: "free", - requestCount: 0, - provider: account.provider, - linkedProviders: [account.provider], - }); - return true; + } catch (error) { + console.error("Error during sign-in:", error); + return false; } - } catch (error) { - console.error("Error during sign-in:", error); - return false; - } - }, - }, + }, async session({ session, user }) { console.log("Session data:", session);