-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
71 lines (59 loc) · 2.03 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { NextResponse, type NextRequest } from "next/server"
import { createMiddlewareClient } from "@supabase/auth-helpers-nextjs"
import { UserPermissionsManager } from "./lib/user-permissions-manager"
const isEmployeesOnlyPage = (pathname: string) => {
return (
pathname.includes("/dashboard") ||
pathname.includes("/add") ||
pathname.includes("/students")
)
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - auth (auth routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - service-worker.js (service worker file)
*/
"/((?!api|auth|_next/static|_next/image|favicon.ico|service-worker.js).*)",
],
}
export async function middleware(req: NextRequest) {
const res = NextResponse.next()
// Create a Supabase client configured to use cookies
const supabase = createMiddlewareClient({ req, res })
// Refresh session if expired - required for Server Components
// https://supabase.com/docs/guides/auth/auth-helpers/nextjs#managing-session-with-middleware
const {
data: { session },
} = await supabase.auth.getSession()
const redirectUrl = req.nextUrl.clone()
if (!session && !req.url.includes("/login")) {
redirectUrl.pathname = "/login"
redirectUrl.searchParams.set(`redirectedFrom`, req.nextUrl.pathname)
return NextResponse.redirect(redirectUrl)
}
const user = session?.user
if (user) {
const permissionsManager = new UserPermissionsManager(user)
if (
isEmployeesOnlyPage(req.nextUrl.pathname) &&
permissionsManager.isStudent()
) {
redirectUrl.pathname = "/student"
return NextResponse.redirect(redirectUrl)
} else if (req.nextUrl.pathname === "/") {
if (permissionsManager.isEmployee()) {
redirectUrl.pathname = "/dashboard"
} else {
redirectUrl.pathname = "/student"
}
return NextResponse.redirect(redirectUrl)
}
}
return res
}