-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
33 lines (29 loc) · 1.01 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// 允许所有来源访问
const response = NextResponse.next();
// No authentication is required if the request path is /login or /api/auth
if (
pathname.startsWith("/login") ||
pathname.startsWith("/api/auth") ||
pathname.match(/\.(.*)$/) // Matches static resources such as.js,.css,.png,.jpg,.jpeg,.svg,.gif, etc
) {
return response;
}
const token = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
});
// If there is no token, redirect to the /login page
// console.log(token, "token");
if (!token) {
const loginUrl = new URL("/login", req.url);
loginUrl.searchParams.set("callbackUrl", req.url);
return NextResponse.redirect(loginUrl);
}
// If there is a token, the request is allowed to continue processing
return response;
}