-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.ts
72 lines (62 loc) · 1.73 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
70
71
72
import type { NextAuthConfig } from 'next-auth';
import { getSession } from '@/app/lib/session';
const canUserGoThere = ({
pathname,
groupId,
userId,
}: {
pathname: string;
groupId: string;
userId: string;
}) => {
return (
pathname === '/dashboard' ||
pathname.includes(`/users/${userId}/edit`) ||
pathname.includes(`/groups/${groupId}/edit`) ||
pathname.includes(`/groups/${groupId}/delete`)
);
};
export const authConfig = {
pages: {
signIn: '/login',
},
callbacks: {
async authorized({ auth, request: { nextUrl } }) {
const isLoggedIn = !!auth?.user;
const isInAdminPanel = nextUrl.pathname.startsWith('/dashboard');
const isOnLoginPage = nextUrl.pathname.startsWith('/login');
const isOnPrintPage = nextUrl.pathname.startsWith('/print');
const session = await getSession();
if (isOnPrintPage) {
if (!isLoggedIn || !session) {
return false;
}
const role = session?.user.role;
if (role !== 'admin') {
return false;
}
}
if (isOnLoginPage && isLoggedIn && session) {
return Response.redirect(new URL('/dashboard', nextUrl));
}
if (isInAdminPanel) {
const userId = session?.user.id;
const groupId = session?.user.groupId;
const role = session?.user.role;
if (!isLoggedIn || !session) {
return false;
}
if (
role === 'admin' ||
canUserGoThere({ groupId, pathname: nextUrl.pathname, userId })
) {
return true;
} else {
return false;
}
}
return true;
},
},
providers: [], // Add providers with an empty array for now
} satisfies NextAuthConfig;