Next.js Discord

Discord Forum

expire a cookie after 30 mins

Answered
Naeemgg posted this in #help-forum
Open in Discord
I'm trying to expire a cookie after 30 mins but I could still see the cookie, tried both expires:1000*60*30 and maxAge:1000*60*30. What am I missing or doing wrong 🧐
Answered by Anay-208
@Naeemgg Upon checking, maxAge is in seconds, and cookies expire after that
View full answer

31 Replies

Giant ichneumon wasp
Some additional details might be able to help further investigation! What are you using to set the cookie?
I believe expires takes a timestamp
Also, I think the expired cookie stays in the list until it is accessed again and at that point it is recognized as expired and cleaned up
@Giant ichneumon wasp Also, I think the expired cookie stays in the list until it is accessed again and at that point it is recognized as expired and cleaned up
I'm setting a cookie in route.ts
import { cookies } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";

export const POST = async (req: NextRequest) => {

  const {name,value,httpOnly,secure,expires,maxAge} = await req.json()

    cookies().set(name,value,{httpOnly,secure,maxAge});
    return NextResponse.json({message:"cookie successfully set."},{status:200})
};
@Giant ichneumon wasp Some additional details might be able to help further investigation! What are you using to set the cookie?
and then making a post request from client to set cookie
 useEffect(()=>{
    const setCookie = async()=>{
      const cookie = await fetch(`/api/cookies/set-cookie`,{
        method: "POST",   
        headers: {
          "Content-Type": "application/json",
        }, 
           body: JSON.stringify({httpOnly:false,secure:false,name:"testing-cookie",value:"value",maxAge:10}), 
      });
    }
    setCookie()
  },[])
I have 2 questions why when I'm setting expires:1000*5 or something so its not getting expired at that time
second is what is the unit of maxAge??
is it miliseconds same as expires or something else
I just want to know how can we delete a cookie n minutes after setting it
It has to be very easy, I know I'm doing something terribly wrong
Bengal
isn't max-age time in seconds? and not in ms?
so you're setting it for 1000*30 minutes instead of 30 minutes, possibly.
and expires is a UTC timestamp, not a number, afaik
expires is supposed to be a timestamp
@Anay-208 expires is supposed to be a timestamp
Tried new Date(Date.now()+1000*60*30) also
@Naeemgg Tried `new Date(Date.now()+1000*60*30)` also
Check the docs on how it’s done
That's how they show it's done
Umm, I'll reproduce in my own environment and let you know by tomorrow
@Naeemgg Upon checking, maxAge is in seconds, and cookies expire after that
Answer
I've confirmed
@Anay-208 I've confirmed
Ok let me try it
I was confused about the unit of maxAge thanks for confirming its in seconfs
@Anay-208 <@782907171721314315> Upon checking, maxAge is in seconds, and cookies expire after that
Mark this message as a solution by right clicking > mark solition
Hey there @Naeemgg is your issue resolved?