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 4, 2024
1 parent d66fadb commit f4ec0f7
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// app/api/auth/[...nextauth]/route.ts
import NextAuth, { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import GitHubProvider from "next-auth/providers/github";
Expand All @@ -18,56 +17,53 @@ const authOptions: NextAuthOptions = {
],
adapter: FirestoreAdapter(adminDb),
callbacks: {
async signIn({ user, account, profile }) {
try {
const userEmail = user.email;
const provider = account?.provider;
async signIn({ user, account, profile }) {
try {
const userEmail = user.email;
const provider = account?.provider;

if (!userEmail) {
throw new Error("No email found for user");
}
if (!userEmail) {
throw new Error("No email found for user");
}

const userDocRef = adminDb.collection("users").doc(userEmail);
const userDoc = await userDocRef.get();
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();

// Link account if provider doesn't match
if (userData.provider && userData.provider !== provider) {
await userDocRef.update({ provider });
// Link account if provider doesn't match
if (userData.provider && userData.provider !== provider) {
await userDocRef.update({ provider });
}
} else {
const newUser = {
email: userEmail,
plan: "free",
requestCount: 0,
provider,
};
await userDocRef.set(newUser);
}
} else {
const newUser = {
email: userEmail,
plan: "free",
requestCount: 0,
provider,
};
await userDocRef.set(newUser);
}

return true;
} catch (error) {
console.error("Sign-in error:", error);
return false;
}
},
},

return true;
} catch (error) {
console.error("Sign-in error:", error);
return false;
}
},
async session({ session, user }) {
session.user.id = user.id;
(link unavailable) = (link unavailable);
return session;
},
},

events: {
async signIn({ user }) {
async onSignIn({ user }) {
console.log("User signed in:", user);
},
},
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };
export { handler as GET, handler as POST };

0 comments on commit f4ec0f7

Please sign in to comment.