Spaces:
Paused
Paused
| import NextAuth from "next-auth" | |
| import { MongoDBAdapter } from "@auth/mongodb-adapter" | |
| import Google from "next-auth/providers/google" | |
| import client from "./lib/adpater"; | |
| // Ensure your environment variables are being read correctly | |
| const allowedEmails = process.env.ALLOWED_EMAILS?.split(',') || []; | |
| export const { handlers, signIn, signOut, auth } = NextAuth({ | |
| adapter: MongoDBAdapter(client), | |
| providers: [Google], | |
| callbacks: { | |
| async signIn({ user, account }) { | |
| // Check if the user's email is in the allowed list | |
| if (account?.provider === "google") { | |
| const email = typeof user?.email === "string" ? user.email : ""; | |
| if (allowedEmails.length > 0 && !allowedEmails.includes(email)) { | |
| return false; // Deny sign-in if email is not allowed | |
| } | |
| return true; // Allow sign-in | |
| } | |
| return true; // Allow sign-in for other providers | |
| } | |
| }, | |
| }) |