forked from MettaChain/PropChain-FrontEnd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
32 lines (25 loc) · 1.09 KB
/
Copy pathmiddleware.ts
File metadata and controls
32 lines (25 loc) · 1.09 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
// Define paths that require authentication
const PROTECTED_ROUTES = ['/dashboard', '/portfolio', '/settings', '/invest'];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Check if the current path is a protected route
const isProtectedRoute = PROTECTED_ROUTES.some(route => pathname.startsWith(route));
if (isProtectedRoute) {
// Look for the auth token in cookies
// This assumes your auth flow sets a cookie named 'auth-token'
const token = request.cookies.get('auth-token')?.value;
if (!token) {
// Redirect to home or login page if no token is found
const loginUrl = new URL('/', request.url);
// Optionally add a redirect parameter to return the user after login
loginUrl.searchParams.set('callbackUrl', pathname);
return NextResponse.redirect(loginUrl);
}
}
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*', '/portfolio/:path*', '/settings/:path*', '/invest/:path*'],
};