Next.js Discord

Discord Forum

`cookie` from `next/headers` `delete()` function is not working?

Unanswered
HOTCONQUEROR posted this in #help-forum
Open in Discord
I have issue with logging out, i am supposed to to delete session cookie both from the browser (application tab) and the database from session document/table

however, i tested cookie from next/headers in api handlers, and the get(), set() are working (in case of login)

but delete() doesn't seem to delete the cookie from browser.

note that i am doing all of this in server, because this is user session and it is marked with httpsOnly.

import { NextRequest, NextResponse } from "next/server";
import { db } from "@/app/db";
import { cookies } from "next/headers";

export async function POST(req: NextRequest) {
  const userSess = (await cookies()).get("session")!.value;

  // nullify the session from the cookies in server
  try {
    await db.session.delete({
      where: {
        token: userSess,
      },
    });
  } catch (e) {
    console.log(e);
    return NextResponse.json({ error: e }, { status: 500 });
  }

  const delCookie = await cookies();
  console.log("logout here");
  console.log(delCookie.get("session"));
  delCookie.delete("session");

  return NextResponse.json(
    { message: "You have been logged out" },
    { status: 200 }
  );
}

0 Replies