-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathauth.config.ts
43 lines (36 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
import type { NextAuthConfig } from 'next-auth';
const publicRoutes = ['/api/document'];
export const authConfig = {
pages: {
signIn: '/login',
newUser: '/',
},
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: {
authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const isOnChat = nextUrl.pathname.startsWith('/');
const isOnRegister = nextUrl.pathname.startsWith('/register');
const isOnLogin = nextUrl.pathname.startsWith('/login');
const isPublicRoute = publicRoutes.some((route) =>
nextUrl.pathname.startsWith(route),
);
if (isLoggedIn && (isOnLogin || isOnRegister)) {
return Response.redirect(new URL('/', nextUrl as unknown as URL));
}
if (isOnRegister || isOnLogin || isPublicRoute) {
return true; // Always allow access to register and login pages or public routes
}
if (isOnChat) {
return isLoggedIn; // Redirect unauthenticated users to login page
}
if (isLoggedIn) {
return Response.redirect(new URL('/', nextUrl as unknown as URL));
}
return true;
},
},
} satisfies NextAuthConfig;