-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
28 lines (20 loc) · 886 Bytes
/
Copy pathmiddleware.js
File metadata and controls
28 lines (20 loc) · 886 Bytes
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
import { NextResponse } from "next/server";
const protectedRoutes = ["/dashboard", "/onboarding", "/roadmap", "/opportunities", "/saved", "/mentor"];
const authRoutes = ["/login", "/signup"];
export function middleware(request) {
const { pathname } = request.nextUrl;
const isProtected = protectedRoutes.some((r) => pathname.startsWith(r));
const isAuthRoute = authRoutes.some((r) => pathname.startsWith(r));
// Check for Firebase auth session cookie
const session = request.cookies.get("__session")?.value;
if (isProtected && !session) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (isAuthRoute && session) {
return NextResponse.redirect(new URL("/dashboard", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};