-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.js
More file actions
31 lines (23 loc) · 826 Bytes
/
middleware.js
File metadata and controls
31 lines (23 loc) · 826 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
30
31
import { NextResponse } from 'next/server';
const PUBLIC_PATHS = ['/login'];
export function middleware(request) {
const { pathname } = request.nextUrl;
// Allow all public paths
if (PUBLIC_PATHS.some((path) => pathname.startsWith(path))) {
return NextResponse.next();
}
// Allow root which handles redirect itself
if (pathname === '/') {
return NextResponse.next();
}
// For protected routes, check token cookie (set on login)
const token = request.cookies.get('ssh_token')?.value;
if (!token) {
const loginUrl = new URL('/login', request.url);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico|android-chrome|apple-touch|favicon|site.webmanifest|default-avatar).*)'],
};