forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
45 lines (38 loc) · 1.23 KB
/
Copy pathmiddleware.js
File metadata and controls
45 lines (38 loc) · 1.23 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
33
34
35
36
37
38
39
40
41
42
43
44
45
import { NextResponse } from 'next/server';
// Routes that require authentication
const PROTECTED = ['/dashboard', '/rewards', '/history', '/settings', '/referral', '/leaderboard'];
// Routes only for unauthenticated users
const AUTH_ONLY = ['/login', '/register', '/forgot-password', '/reset-password', '/verify-email'];
export function middleware(request) {
const { pathname } = request.nextUrl;
const token = request.cookies.get('authToken')?.value;
const isProtected = PROTECTED.some((p) => pathname.startsWith(p));
const isAuthOnly = AUTH_ONLY.some((p) => pathname.startsWith(p));
if (isProtected && !token) {
const url = request.nextUrl.clone();
url.pathname = '/login';
url.searchParams.set('redirect', pathname);
return NextResponse.redirect(url);
}
if (isAuthOnly && token) {
const url = request.nextUrl.clone();
url.pathname = '/dashboard';
return NextResponse.redirect(url);
}
return NextResponse.next();
}
export const config = {
matcher: [
'/dashboard/:path*',
'/rewards/:path*',
'/history/:path*',
'/settings/:path*',
'/referral/:path*',
'/leaderboard/:path*',
'/login',
'/register',
'/forgot-password',
'/reset-password',
'/verify-email',
],
};