Added email domain restriction and error popup toast

This commit is contained in:
arav
2026-01-08 23:44:21 +05:30
parent 91a3f95a0a
commit 6cc3e4a086
5 changed files with 56 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
import NextAuth, { type DefaultSession } from 'next-auth';
import type { NextAuthConfig } from 'next-auth';
import Google from "next-auth/providers/google";
import Google from 'next-auth/providers/google';
import { db, admins, students } from '@workspace/db';
import { eq } from '@workspace/db/drizzle';
@@ -12,7 +12,7 @@ declare module 'next-auth' {
studentId?: number;
completedProfile?: boolean;
[key: string]: any;
} & DefaultSession["user"];
} & DefaultSession['user'];
}
interface JWT {
@@ -32,6 +32,13 @@ declare module 'next/server' {
const authConfig: NextAuthConfig = {
providers: [Google],
callbacks: {
async signIn({ user }) {
const allowedDomain = process.env.ALLOWED_EMAIL_DOMAIN;
if (allowedDomain && user.email && !user.email.endsWith(allowedDomain)) {
return false;
}
return true;
},
async jwt({ token, account, user }) {
// Only set role, adminId, studentId, and email in JWT
const email = user?.email || token?.email;
@@ -94,6 +101,9 @@ const authConfig: NextAuthConfig = {
return session;
},
},
pages: {
error: '/login',
},
};
// Note: TypeScript warnings about inferred types are expected with NextAuth v5 beta