-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
40 lines (30 loc) · 1.29 KB
/
middleware.ts
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
/**
* Middleware allows you to run code before a request is completed,
* then based on the incoming request, you can modify the response by rewriting,
* redirecting, adding headers, or setting cookies.
* @https://nextjs.org/docs/advanced-features/middleware
*/
import { NextResponse } from 'next/server';
export function middleware(req) {
const { pathname } = req.nextUrl;
const authCookie = req.cookies.get('SITE_DATA_LOGIN_COOKIE');
// Determine whether you have permission to enter the dashboard
// If you use Docker, please do not directly use the server jump `/`
// such as: /dir/mypage@admin
//----------------------------------------------------------------
// if (
// /\/\d+\/(.*?)\@admin/.test(pathname) ||
// ) {
// }
//----------------------------------------------------------------
if (pathname.startsWith('/dashboard')) {
// If user is not logged in, return login component
if (!authCookie) return NextResponse.redirect(new URL("/sign-in.html", req.nextUrl));
}
//----------------------------------------------------------------
const response = NextResponse.next();
if (authCookie !== undefined) {
response.headers.set('Authorization', `JWT ${authCookie.value}`);
}
return response;
}