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 6, 2024
1 parent 232c424 commit 7f9277e
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,72 @@ const authOptions: NextAuthOptions = {
],
adapter: FirestoreAdapter(adminDb),
callbacks: {
async signIn({ user, account }) {
const userEmail = user.email;
const provider = account.provider;
async signIn({ user, account }) {
const userEmail = user.email;
const provider = account.provider;

if (!userEmail || !provider) return false;
if (!userEmail || !provider) return false;

try {
const userDocRef = adminDb.collection("users").doc(userEmail);
const userDoc = await userDocRef.get();
try {
const userDocRef = adminDb.collection("users").doc(userEmail);
const userDoc = await userDocRef.get();

if (userDoc.exists) {
const userData = userDoc.data();
if (userDoc.exists) {
const userData = userDoc.data();

if (!userData) {
console.error("User data not found");
return false;
}

console.log("Existing user signing in:", userData);
if (!userData) {
console.error("User data not found");
return false;
}

if (userData.provider && userData.provider !== provider) {
const linkedProviders = userData.linkedProviders || [];
if (!linkedProviders.includes(provider)) {
linkedProviders.push(provider);
await userDocRef.update({ linkedProviders });
}
} else if (!userData.provider) {
await userDocRef.update({ provider });
}
console.log("Existing user signing in:", userData);

// Check if provider is already linked
if (userData.linkedProviders && userData.linkedProviders.includes(provider)) {
return true;
} else {
console.log("New user signing up:", { email: userEmail, provider });

await userDocRef.set({
email: userEmail,
plan: "free",
requestCount: 0,
provider,
linkedProviders: [provider],
});
}

// Link provider to existing account
if (userData.provider && userData.provider !== provider) {
const linkedProviders = userData.linkedProviders || [];
if (!linkedProviders.includes(provider)) {
linkedProviders.push(provider);
await userDocRef.update({ linkedProviders });
}
return true;
}
} catch (error) {
if (error.code === 'auth/invalid-email') {
console.error('Invalid email:', error);
} else {
console.error('Sign-in error:', error);

// Handle new provider for existing user without provider
if (!userData.provider) {
await userDocRef.update({ provider, linkedProviders: [provider] });
return true;
}

// If none of the above conditions are met, return error
console.error("OAuthAccountNotLinked error");
return false;
} else {
// Create new user document
console.log("New user signing up:", { email: userEmail, provider });
await userDocRef.set({
email: userEmail,
plan: "free",
requestCount: 0,
provider,
linkedProviders: [provider],
});
return true;
}
},
} catch (error) {
if (error.code === 'auth/invalid-email') {
console.error('Invalid email:', error);
} else {
console.error('Sign-in error:', error);
}
return false;
}
},

async session({ session, user }) {
console.log("Session data:", session);
session.user.id = user.id;
Expand Down

0 comments on commit 7f9277e

Please sign in to comment.