-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.ts
69 lines (60 loc) · 2.09 KB
/
auth.config.ts
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
import { paths } from "@/lib/constants";
import { Role } from "@prisma/client";
import { NextAuthConfig } from "next-auth";
export const authConfig = {
pages: {
signIn: paths.toHome,
},
callbacks: {
async jwt({ token, trigger, session, user }) {
if (trigger === "update" && session) {
return { ...token, ...session?.user };
}
return { ...token, ...user };
},
async session({ session, token }) {
if (token.sub != null) {
session.user.id = token.sub;
}
session.user.pseudo = token.pseudo;
session.user.date = token.date;
session.user.score = token.score;
session.user.role = token.role;
session.user.completed = token.completed;
session.user.surveyCompleted = token.surveyCompleted;
return session;
},
authorized({ auth, request: { nextUrl } }) {
const isUserLoggedIn = auth?.user.role === Role.user;
const isAdminLoggedIn = auth?.user.role === Role.admin;
const isOnWheel = nextUrl.pathname.startsWith(paths.toWheel);
const isOnHome = nextUrl.pathname === paths.toHome;
const isOnAdmin = nextUrl.pathname === paths.toAdmin;
const isOnAdminLogin = nextUrl.pathname === paths.toAdminLogin;
// Authorize logging page for all users
if (isOnAdminLogin) {
// Redirect logged admin to the dashboard
if (isAdminLoggedIn)
return Response.redirect(new URL(paths.toAdmin, nextUrl));
return true;
}
// Authorize only admins
if (isOnAdmin) return isAdminLoggedIn;
// Redirect logged user to /wheel
if (isOnHome && isUserLoggedIn)
return Response.redirect(new URL(paths.toWheel, nextUrl));
// Handle /wheel auth
if (isOnWheel) {
// Redirect logged admin to the dashboard
if (isAdminLoggedIn)
return Response.redirect(new URL(paths.toAdmin, nextUrl));
return isUserLoggedIn;
}
return true;
},
},
session: { strategy: "jwt", maxAge: 24 * 60 * 60 },
providers: [],
trustHost: true,
secret: process.env.AUTH_SECRET,
} satisfies NextAuthConfig;