-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
54 lines (47 loc) · 2.01 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
54
import { getToken } from 'next-auth/jwt'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const { pathname }: { pathname: string } = request.nextUrl
const token = await getToken({ req: request })
const response = await fetch(`http://127.0.0.1:3000/api/v1/AUTH/global/user/` + token?.email)
const data = await response.json()
const user = data?.user
const Redirect = () => {
if (user?.role === 'ADMIN') {
return NextResponse.redirect(new URL('/', request.url))
} else if (user?.role === 'LECTURER') {
return NextResponse.redirect(new URL('/', request.url))
} else if (user?.role === 'STUDENT') {
return NextResponse.redirect(new URL('/', request.url))
} else {
return NextResponse.redirect(new URL('/auth/signin', request.url))
}
}
//PAGE ACCESS MIDDLEWARE
if (token) {
if (pathname.includes('/dashboard/admin') && user?.role !== 'ADMIN') {
return Redirect()
}
} else {
if (pathname.includes('/dashboard/admin')) return Redirect()
if (pathname.includes('/dashboard/lecturer')) return Redirect()
if (pathname.includes('/dashboard/view/profileSettings')) return Redirect()
// if (pathname.includes('/dashboard/student')) return Redirect()
}
//API ACCESS MIDDLEWARE
if (token) {
// if (pathname.includes('/dashboard/admin') && user?.role != 'ADMIN') {
// return Redirect()
// }
} else {
// if (pathname.includes('/dashboard/admin')) return Redirect()
// if (pathname.includes('/dashboard/lecturer')) return Redirect()
// if (pathname.includes('/dashboard/view/profileSettings')) return Redirect()
// if (pathname.includes('/dashboard/student')) return Redirect()
}
}
export const config = {
matcher: ['/auth/signin', '/dashboard/:path*', '/teacher/:path*', '/student/:path*', '/api/admin/:function*', '/api/teacher/:function*', '/api/student/:function*'],
}