-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
46 lines (39 loc) · 1.41 KB
/
middleware.ts
File metadata and controls
46 lines (39 loc) · 1.41 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
43
44
45
46
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
export async function middleware(request: NextRequest) {
const sessionId = request.cookies.get("session_id")?.value
const { pathname } = request.nextUrl
// Public routes that don't require authentication
const publicRoutes = ["/", "/login", "/register"]
// Check if the route is public
if (publicRoutes.some((route) => pathname === route)) {
// If user is already logged in, redirect to appropriate dashboard
if (sessionId) {
// We can't check the role here, so we'll redirect to a route handler that will check
return NextResponse.redirect(new URL("/api/auth/redirect", request.url))
}
return NextResponse.next()
}
// Protected routes
if (!sessionId) {
return NextResponse.redirect(new URL("/", request.url))
}
// Role-specific routes
if (pathname.startsWith("/job-seeker") || pathname.startsWith("/employer")) {
// We'll let the layout components handle role-specific access
return NextResponse.next()
}
return NextResponse.next()
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!api/auth/redirect|_next/static|_next/image|favicon.ico).*)",
],
}