Netlify function can't set cookie

Hi,

As the title says, I’m unable to set a cookie with a Netlify function. Here’s my code:

export default (request, context) => {
  const cookieName = 'cookie-name';

  context.cookies?.set({
    name: cookieName,
    value: 'hello',
  });

  const value = context.cookies?.get(cookieName);
  return new Response(`Cookie value has been set. Value: ${value}`);
};

Gr,

Andy

Have you tried setting the domain option to your Netlify subdomain? If not, the cookie will be rejected by the browsers.

Yes, and that isn’t working either.

@hrishikesh here’s my code, in a regular function:

export default (request, context) => {
  const cookieName = 'cookie-name';

  try {
    context.cookies.set({
      domain: context.site.url,
      name: cookieName,
      value: 'hello',
    });

    const value = context.cookies.get(cookieName);
    return new Response(`Cookie value has been set. Value: ${value}`);
  } catch (err) {
    return new Response(`Cookie value has not been set. Error: ${err}`);
  }
};

export const config = {
  path: '/set-cookie',
};

Gr,

Andy

Please share a link to reproduce the issue.

I’m currently faced with same problem of cookie been rejected by browser, error:

Cookie has been rejected for invalid domain.

when i inspect the network the cookie is visible but browser rejects it. i have tried removing the domain and i get error:

Cookie does not have a proper “SameSite” attribute value. Soon, cookies without the “SameSite” attribute or with an invalid value will be treated as “Lax”. This means that the cookie will no longer be sent in third-party contexts. If your application depends on this cookie being available in such contexts, please add the “SameSite=None“ attribute to it. To know more about the “SameSite“ attribute, read Set-Cookie - HTTP | MDN

import { Router } from "express";
import isRegistered from "../lib/registerUser/isRegistered.js";
import bcryptCompare from "../lib/bycrpt/compare.js";
import signUser from "../lib/jwt/signUser.js";
import cookieParser from "cookie-parser";
const login = Router();
login.use(cookieParser());

login.post("/login", async (req, res) => {
  console.log(req.header);
  const userData = req.body;
  try {
    const isExistingUser = await isRegistered(userData);
    if (!isExistingUser) {
      res.send(false);
      return;
    }
    const compareUserPassword = await bcryptCompare(
      userData.password,
      isExistingUser.password
    );

    if (isExistingUser && compareUserPassword) {
      const code = isExistingUser.code;
      const codeType = code.split(":")[1];

      try {
        const jwtSign = signUser(userData.email);

        res.cookie("jwt", jwtSign, {
          sameSite: "None",
          secure: true,
          domain: ".dainty-selkie-5509f1.netlify.app",
        });
        if (codeType === "u") {
          res.send("user");
        } else {
          codeType === "a";
          res.send("admin");
        }
      } catch (err) {
        console.log(err);
      }
    } else {
      res.send(false);
    }
  } catch (err) {
    res.send(false);
    console.log(err);
  }
});

export default login;

This is not a Netlify issue and a setup with your code that you might need to solve yourself. This has worked for me in the past: netlify-file-browser/netlify/functions/routes/callback.js at v2 · Hrishikesh-K/netlify-file-browser (github.com)