如何在nextjs中间件中删除cookie



无法在nextjs中间件中删除cookie。尝试如下:

import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
export function middleware(req: NextRequest, ev: NextFetchEvent) {
const cookie = req.cookies.get('access_token')?.value;
const headers: any = req.headers;
const response = NextResponse.next();
console.log('req.headers ', headers.get('logout'))
if (headers.get('logout')) {
console.log(cookie);
response.cookies.delete('access_token');
console.log(req.cookies.get('access_token')?.value); // still logs the cookie
}
return response;
}

如果你使用cookies-next库,它有deleteCookie

import { getCookie,deleteCookie } from "cookies-next";
// its type
export declare const deleteCookie: (key: string, options?: OptionsType) => void;

如何使用cookies-next

相关内容

  • 没有找到相关文章

最新更新