-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.config.ts
More file actions
73 lines (69 loc) · 2.29 KB
/
Copy pathauth.config.ts
File metadata and controls
73 lines (69 loc) · 2.29 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { NextAuthConfig } from "next-auth";
/**
* Edge-safe Auth.js config.
*
* This half contains ONLY code that can run in the Edge runtime (no Prisma, no
* bcrypt). `middleware.ts` builds a NextAuth instance from this to verify the
* JWT session and enforce route protection. The Node-only half (Credentials +
* Prisma adapter) lives in `auth.ts`.
*/
// Route prefixes that require an authenticated session.
const PROTECTED_PREFIXES = [
"/dashboard",
"/learn",
"/machines",
"/paths",
"/certs",
"/profile",
"/settings",
"/writeups",
"/teams",
"/admin",
];
export const authConfig = {
pages: {
signIn: "/login",
},
session: { strategy: "jwt" },
providers: [], // populated in auth.ts (Node runtime)
callbacks: {
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const isProtected = PROTECTED_PREFIXES.some((p) =>
nextUrl.pathname.startsWith(p),
);
// Admin area additionally requires the ADMIN role.
if (nextUrl.pathname.startsWith("/admin")) {
return isLoggedIn && auth?.user?.role === "ADMIN";
}
if (isProtected) return isLoggedIn;
return true;
},
jwt({ token, user, trigger, session }) {
if (user) {
token.id = user.id as string;
token.username = (user.username as string) ?? token.username;
token.role = (user.role as JwtRole) ?? "USER";
token.subscriptionTier = (user.subscriptionTier as JwtTier) ?? "FREE";
}
// Allow client-side session.update() to refresh cached fields.
if (trigger === "update" && session?.user) {
token.username = session.user.username ?? token.username;
token.subscriptionTier = session.user.subscriptionTier ?? token.subscriptionTier;
}
return token;
},
session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
session.user.username = token.username as string;
session.user.role = token.role as JwtRole;
session.user.subscriptionTier = token.subscriptionTier as JwtTier;
}
return session;
},
},
} satisfies NextAuthConfig;
// Local aliases to avoid importing Prisma enums into the edge bundle.
type JwtRole = "USER" | "CREATOR" | "ADMIN";
type JwtTier = "FREE" | "PRO" | "TEAM";