-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.ts
61 lines (54 loc) · 1.39 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
import type {NextAuthConfig} from 'next-auth';
import {v4 as uuidv4} from 'uuid';
const authConfig = {
pages: {
signIn: '/auth/signin',
},
callbacks: {
async authorized({request: {nextUrl}, auth}) {
const isLoggedIn = !!auth?.user;
if (isLoggedIn) {
if (nextUrl.pathname === '/auth/signin') {
return Response.redirect(new URL('/', nextUrl));
}
if (nextUrl.pathname === '/admin') {
if (!auth.isAdmin) {
return Response.redirect(
new URL('/auth/error?error=accessdenied', nextUrl)
);
}
}
return true;
}
return false;
},
async jwt({token, user}) {
// @todo: Add in hook for verififing email
if (user) {
token.emailVerified = user.emailVerified;
token.isAdmin = user.isAdmin;
token.email = user.email;
}
return token;
},
async session({session, token}) {
session.isAdmin = token.isAdmin;
if (session.user !== undefined && token.email) {
session.user.email = token.email;
if (token.sub) {
session.user.id = token.sub;
}
}
return session;
},
},
providers: [],
session: {
strategy: 'jwt',
maxAge: 60 * 60, // 1 hour
generateSessionToken: () => {
return uuidv4();
},
},
} satisfies NextAuthConfig;
export default authConfig;