I am learning Next.js - version 13 and I try to customize the next.js app base on the standard document. But somehow, the middleware is not called. I assume I do something wrong here. If you have a time, please review the issue.
Here is the code change of middleware.ts:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { AUTHORIZATION_TOKEN, COOKIE_TOKEN } from "@libs/constants";
import { verify } from "@libs/token";
export const config = {
matcher: ["/admin/:path*", "/signin", "/api/:path*"],
};
export async function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
const regex = new RegExp('\\/api\\/(category|product|cart|coupon|auth)\\/(create|update|delete)', 'i')
let matcher: any = regex.exec(url.pathname);
let token: any;
let isValidToken: any;
if (matcher && matcher[0]) {
token = request.headers.get(AUTHORIZATION_TOKEN);
isValidToken = await verify(token);
} else {
token = request.cookies.get(COOKIE_TOKEN)?.value;
if (token) {
isValidToken = await verify(JSON.parse(token));
}
if (url.pathname.startsWith("/admin")) {
if (isValidToken) {
return NextResponse.next();
} else {
url.pathname = "/signin";
return NextResponse.redirect(url);
}
}
if (url.pathname.startsWith("/signin") && isValidToken) {
url.pathname = "/admin";
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
And the structure of project:
enter image description here
Does someone get any suggestions in this case? If I am wrong, please correct me. Thank you so much.
I am learning Next.js - version 13 and I try to customize the next.js app base on the standard document. But somehow, the middleware is not called. I assume I do something wrong here. If you have a time, please review the issue.
Here is the code change of middleware.ts:
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { AUTHORIZATION_TOKEN, COOKIE_TOKEN } from "@libs/constants";
import { verify } from "@libs/token";
export const config = {
matcher: ["/admin/:path*", "/signin", "/api/:path*"],
};
export async function middleware(request: NextRequest) {
const url = request.nextUrl.clone();
const regex = new RegExp('\\/api\\/(category|product|cart|coupon|auth)\\/(create|update|delete)', 'i')
let matcher: any = regex.exec(url.pathname);
let token: any;
let isValidToken: any;
if (matcher && matcher[0]) {
token = request.headers.get(AUTHORIZATION_TOKEN);
isValidToken = await verify(token);
} else {
token = request.cookies.get(COOKIE_TOKEN)?.value;
if (token) {
isValidToken = await verify(JSON.parse(token));
}
if (url.pathname.startsWith("/admin")) {
if (isValidToken) {
return NextResponse.next();
} else {
url.pathname = "/signin";
return NextResponse.redirect(url);
}
}
if (url.pathname.startsWith("/signin") && isValidToken) {
url.pathname = "/admin";
return NextResponse.redirect(url);
}
}
return NextResponse.next();
}
And the structure of project:
enter image description here
Does someone get any suggestions in this case? If I am wrong, please correct me. Thank you so much.
Share Improve this question edited Feb 12, 2023 at 14:14 Em Ha Tuan asked Feb 12, 2023 at 6:04 Em Ha TuanEm Ha Tuan 1033 silver badges10 bronze badges1 Answer
Reset to default 8I had similar issue. I've moved my middleware.ts
file in src
folder like: /src/middleware.ts
and now it's getting called.
According your screenshot, it looks like your middleware.ts is out of /src
folder.
Also keep in mind you can't do IO operations like calling DB in that middleware function. Still you can await promises or calling other services via fetch
.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313101a4420294.html
评论列表(0条)