-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmiddleware.ts
53 lines (43 loc) · 1.31 KB
/
middleware.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
import { withAuth } from 'next-auth/middleware';
// More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
//Root level paths that mentors have access to OVER regular users
//For example, "/" is accessible by mentors, but users can also access it, so we DO NOT include it.
const mentorPaths = ['/mentor', '/dashboard'];
export default withAuth({
callbacks: {
async authorized({ req, token }) {
const { pathname } = req.nextUrl;
if (!token || !token.email) {
return false;
}
const response = await fetch(
'https://helpr.tamuhack.org/api/users/getRoles?' +
new URLSearchParams({
email: token.email,
})
);
let data = { isAdmin: false, isMentor: true };
try {
data = await response.json();
} catch (e) {
console.log('Middleware error', e);
}
if (data.isAdmin) {
return true;
}
mentorPaths.forEach((path) => {
if (pathname.includes(path)) {
return data.isMentor;
}
});
return !!token;
},
},
pages: {
signIn: '/login',
},
});
//Paths to run this middleware on (all paths that require a user to be signed in)
export const config = {
matcher: ['/admin', '/mentor', '/', '/dashboard/:path*'],
};