我正在我的 nextjs 组件之一中设置编码的 cookie
setCookie("_SYS_USER_AUTH", window.btoa(res?.data));
我想在 Nextjs 中间件中访问相同的内容
export function middleware(request) {
const { cookies, url } = request;
const jwt = cookies.get(`_SYS_USER_AUTH`);
if (request.nextUrl.pathname.startsWith("/account")) {
console.log(window.btoa(jwt));
}
}
这里我收到窗口未定义错误。有没有其他方法可以在 nextjs 中间件中解码它?
我有点晚了,但这是一个适用于我正在开发的项目的实现(Next.js 13)
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const accessToken = request.cookies.get(COOKIE_TOKEN)?.value
const operationId = accessToken ? (decodeJWT(accessToken) as Token).operationCompanyId : undefined
if (operationId !== '1') {
return NextResponse.redirect(new URL('/404', request.url))
}