-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
29 lines (23 loc) · 928 Bytes
/
middleware.ts
File metadata and controls
29 lines (23 loc) · 928 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
29
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Allow open routes (no auth required)
if (pathname === '/login' || pathname === '/signup') {
return NextResponse.next();
}
// Mock example of checking user session - adjust based on your auth mechanism
// Example: check cookie or headers for a token/session
const token = request.cookies.get('authToken')?.value;
// If no token, redirect to /login
if (token) {
const loginUrl = request.nextUrl.clone();
loginUrl.pathname = '/login';
return NextResponse.redirect(loginUrl);
}
// Allow access if token exists
return NextResponse.next();
}
// Specify paths where middleware will run if using next.config.js setup
export const config = {
matcher: ['/((?!_next|favicon.ico).*)'], // applies to all routes except _next and favicon.ico
};