Repository navigation
Expand file tree
/
Copy pathauth.ts
More file actions
45 lines (43 loc) · 1.77 KB
/
Copy pathauth.ts
File metadata and controls
45 lines (43 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/lib/prisma";
/** Only accounts on this domain can sign in. */
const ALLOWED_DOMAIN = (process.env.ALLOWED_EMAIL_DOMAIN ?? "nitjsr.ac.in").toLowerCase();
export const { handlers, auth, signIn, signOut } = NextAuth({
trustHost: true,
adapter: PrismaAdapter(prisma),
// Database sessions: a role change (e.g. demoting an admin) takes effect on
// the very next request instead of waiting for a JWT to expire.
session: { strategy: "database" },
secret: process.env.AUTH_SECRET ?? process.env.NEXTAUTH_SECRET,
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID ?? process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET ?? process.env.GOOGLE_CLIENT_SECRET,
// Seeded users already have a User row but no linked Google account yet.
// Linking by email is safe here: Google verifies the address and we only
// accept the college domain (checked again in `signIn` below).
allowDangerousEmailAccountLinking: true,
// Google's account picker only offers college accounts. This is a UX
// hint only; the real check is in `signIn` below.
authorization: { params: { hd: ALLOWED_DOMAIN, prompt: "select_account" } },
}),
],
pages: {
signIn: "/admin/login",
error: "/admin/login",
},
callbacks: {
signIn({ profile }) {
const email = profile?.email?.toLowerCase();
if (!email || profile?.email_verified === false) return false;
return email.endsWith("@" + ALLOWED_DOMAIN);
},
session({ session, user }) {
session.user.id = user.id;
session.user.role = user.role;
return session;
},
},
});