#Partitioned cookie with Express

1 messages · Page 1 of 1 (latest)

tame imp
#

I have an application using NestJS that performs authentication using cookies with a certain configuration.
In production, everything works normally. The problem starts when I try to access my frontend (on localhost), directly accessing my nestjs staging server. I dug around looking for solutions after having tried almost everything.

The solution is Partitioned cookies which the current version of express does not support.
Does anyone know a workaround to set a cookie using NestJS/express?

#
export const generateCookieConfig = (
  maxAge: number,
  isLocalhostOrigin: boolean,
): CookieOptions => {
  const props: CookieOptions = {
    httpOnly: true,
    sameSite: process.env.NODE_ENV === 'production' ? 'strict' : 'none',
    secure: true,
    maxAge,
  };

  if (isLocalhostOrigin) {
    if (process.env.NODE_ENV === 'staging') {
      props.httpOnly = true;
      props.secure = true;
      props.sameSite = 'none';
    }

    return props;
  }
  if (process.env.IS_LOCALHOST) return props;

  props.domain = process.env.FRONT_END_URL;

  return props;
};

export function generateAuthenticationCookiesOn(
  response: Response,
  token: SignInToken,
  store: AuthorUserContext,
) {
  const { accessToken, refreshToken } = token.value;

  const isLocalhostOrigin =
    store?.userDnsOrigin?.includes('localhost') ?? false;

  const acCookies = generateCookieConfig(minutes(60), isLocalhostOrigin);
  response.cookie(AccessTokenCookieKey, accessToken, acCookies);

  if (refreshToken) {
    response.cookie(
      RefreshTokenCookieKey,
      refreshToken,
      generateCookieConfig(RefreshToken.EXPIRES_AT, isLocalhostOrigin),
    );
  }
}

#

from express