-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
37 lines (27 loc) · 1.29 KB
/
middleware.js
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
// middleware.js
import { NextResponse } from 'next/server';
export async function middleware(req) {
const sessionId = req.cookies.get('session_id'); // Get session ID from cookies
console.log('Session ID:', sessionId); // Log for debugging
// If no session ID found, redirect to login
if (!sessionId) {
console.log('No session ID found, redirecting to login...');
return NextResponse.redirect(new URL('/', req.url));
}
// Ensure the sessionId is a string
const sessionIdValue = typeof sessionId === 'string' ? sessionId : sessionId.value;
// Validate session via API call
const sessionCheckResponse = await fetch(`${req.nextUrl.origin}/api/validate-session?session_id=${sessionIdValue}`);
// Check if the response indicates a valid session
if (!sessionCheckResponse.ok) {
console.log('Invalid session ID, redirecting to login...');
return NextResponse.redirect(new URL('/', req.url));
}
console.log('Valid session found, proceeding to the dashboard...');
// If session is valid, allow the request to proceed
return NextResponse.next();
}
// Specify paths where middleware should apply
export const config = {
matcher: ['/dashboard/:path*', '/otherProtectedRoute/:path*'], // Add paths that need protection
};