-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
42 lines (36 loc) · 1.15 KB
/
middleware.ts
File metadata and controls
42 lines (36 loc) · 1.15 KB
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
import NextAuth from "next-auth"
import { authConfig } from "@/auth.config"
const { auth } = NextAuth(authConfig)
export default auth((req) => {
const isLoggedIn = !!req.auth
const isOnDashboard = req.nextUrl.pathname.startsWith("/dashboard")
const isOnOnboarding = req.nextUrl.pathname.startsWith("/onboarding")
const isOnAdmin = req.nextUrl.pathname.startsWith("/admin")
const onboardingCompleted = req.auth?.user?.onboardingCompleted
const userRole = req.auth?.user?.role
if (isOnAdmin) {
if (!isLoggedIn) {
return Response.redirect(new URL("/login", req.nextUrl))
}
if (userRole !== "ADMIN") {
return Response.redirect(new URL("/dashboard", req.nextUrl))
}
return
}
if (isOnDashboard) {
if (isLoggedIn) {
return
}
return Response.redirect(new URL("/login", req.nextUrl))
}
if (isOnOnboarding) {
if (!isLoggedIn) {
return Response.redirect(new URL("/login", req.nextUrl))
}
// Allow access to /onboarding for all logged-in users so they can generate new roadmaps
return
}
})
export const config = {
matcher: ["/dashboard/:path*", "/onboarding/:path*", "/admin/:path*"],
}