-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
39 lines (32 loc) · 1.51 KB
/
Copy pathmiddleware.js
File metadata and controls
39 lines (32 loc) · 1.51 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
import { auth } from "./lib/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { pathname } = req.nextUrl;
const session = req.auth;
const role = session?.user?.role;
// Redirect logged-in users away from auth pages
if ((pathname === "/login" || pathname === "/register") && session) {
if (role === "employer") return NextResponse.redirect(new URL("/employer/dashboard", req.url));
if (role === "admin") return NextResponse.redirect(new URL("/admin", req.url));
return NextResponse.redirect(new URL("/dashboard", req.url));
}
// Protect dashboard — jobseeker only
if (pathname.startsWith("/dashboard")) {
if (!session) return NextResponse.redirect(new URL(`/login?callbackUrl=${pathname}`, req.url));
if (role !== "jobseeker") return NextResponse.redirect(new URL("/", req.url));
}
// Protect employer — employer only
if (pathname.startsWith("/employer")) {
if (!session) return NextResponse.redirect(new URL(`/login?callbackUrl=${pathname}`, req.url));
if (role !== "employer") return NextResponse.redirect(new URL("/", req.url));
}
// Protect admin — admin only
if (pathname.startsWith("/admin")) {
if (!session) return NextResponse.redirect(new URL(`/login?callbackUrl=${pathname}`, req.url));
if (role !== "admin") return NextResponse.redirect(new URL("/", req.url));
}
return NextResponse.next();
});
export const config = {
matcher: ["/dashboard/:path*", "/employer/:path*", "/admin/:path*", "/login", "/register"],
};