-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
45 lines (41 loc) · 1.29 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
import type {NextAuthConfig } from 'next-auth';
// import NextAuthOptions from 'next-auth';
export const authConfig = {
pages: {
signIn: '/login',
},
providers: [
// added later in auth.ts since it requires bcrypt which is only compatible with Node.js
// while this file is also used in non-Node.js environments
],
callbacks: {
async session({ session, token }) {
// if (token?.accessToken) {
// session.accessToken = token.accessToken
// }
return session
},
async jwt({ token, user }) {
if (user) {
token.id = user.id;
}
return token;
},
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
console.log("auth "+auth);
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard');
if (isOnDashboard) {
if (isLoggedIn) return true;
return false; // Redirect unauthenticated users to login page
} else if (isLoggedIn) {
return Response.redirect(new URL('/dashboard', nextUrl));
}
return true;
},
},
session: {
maxAge: 12 * 60 * 60, // Session will be valid for 24 hours (86400 seconds)
updateAge: 6 * 60 * 60, // Session will be updated if 12 hours have passed (43200 seconds)
},
} satisfies NextAuthConfig;