Skip to content

Commit

Permalink
Update route.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ArhanAnsari authored Nov 27, 2024
1 parent c9df67f commit 8172704
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8172704

Please sign in to comment.