如何在nextjs中间件中删除cookie

6vl6ewon  于 2023-04-05  发布在  其他
关注(0)|答案(1)|浏览(180)

无法删除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;
}
lnvxswe2

lnvxswe21#

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

import { getCookie,deleteCookie } from "cookies-next";

// its type
export declare const deleteCookie: (key: string, options?: OptionsType) => void;

如何使用cookies-下一页

相关问题