Search code examples
javascriptwebsveltesveltekit

SvelteKit cookies disappearing upon site refresh, and returning the old value after an irrelevant API call


I am revamping my website using svelte(kit), but running into a very strange issue, whenever /api/signup/delivercode is called with a correct code, an account cookie is set, with no time expiry, when I refresh the page, the cookie disappears, and then when I send a request to /api/signup after it disappeared, the OLD cookie reappears (at this point there isn't supposed to be any cookie yet (keep in mind it only appears when the signup api is called), only once the delivercode endpoint is triggered. Does anyone have any idea what could be going wrong and why?

https://streamable.com/amc9t4 /api/signup endpoint:

export const POST = async (event) => {
    // more code
    let response = await schemas.account.create(info).then(() => apiConsts.mail(event.cookies, info)) // I made sure with ctrl+f, this is the ONLY line where cookies are mentioned
    // more code
}

apiConsts.mail function:

    mail: async (cookies, data, signup) => { // should move entire mail-code system to redis instead of mongo
        // more code
        cookies.set(`concode`, browserCode, { httpOnly: true, sameSite: `strict`, maxAge: 1000 * 60 * 30 }) // same here, this is the only place where the cookies object is mentioned
        // more code
    },

despite the fact that that those are the only locations where cookies are referenced in my code, the /api/signup sends an account cookie as well (see in first attached image), the cookie value is one that my code set beforehand (you can check the timeid, the first number in the start is the date in ms when it was created), it was just not visible in my browser nor with event.cookies.get() meanwhile, and it disappears again when I refresh (second attached video)

https://streamable.com/v6a27g
receiving cookies

Am I doing something wrong? Or is this a SvelteKit bug?


Solution

  • The solution was quite simple: I had to pass the path key with a value of / in the settings object of the cookies.set function, this made it universal cookies that stick around no matter at what subpage you are located.