Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookies not setting in production #98

Open
adamkb33 opened this issue Nov 14, 2023 · 5 comments
Open

Cookies not setting in production #98

adamkb33 opened this issue Nov 14, 2023 · 5 comments

Comments

@adamkb33
Copy link

Hi am i using you cookie parser library in my nest js project. I am manly using your library for authentication.

I am trying to set a cookie called "jwt" when user has completed the authentication process. This is how the code looks like,

    response.cookie('jwt', accessToken, {
      maxAge: 15 * 60 * 1000,
      httpOnly: true,
      secure: env === 'production' ? true : false,
    });

This works as expected inn localhost but when i deploy the code it does not set the cookie. Also no errors or warnings are fired.
I have messed around allot with the options but i could not set the cookie.

This is not a issue with your package i am just looking for guidance on how to handle this since you have worked with this. I have searched everywhere on why cookies are not setting but there is no concrete answer. Most of the answers are suggestions on modifying the options.

Sorry if this is a stupid question, but i have had trouble with this for 2 weeks now. If this is not a appropriate question, just let me know and i will take it down.

Thanks!

@dougwilson
Copy link
Contributor

It is probably no an issue with this package, as this package has no functionality to set cookies at all. It just reads the incoming Cookie header and places them in to req.cookies. All of the API is documented in the README here. Are you getting a Cookie header from the client?

@adamkb33
Copy link
Author

adamkb33 commented Nov 14, 2023

Thanks for answering even if this has nothing to do with the package... I just have struggled to understand how setting cookies and getting cookies works.

I am getting the cookie to the client. But it is not setting the cookie. My first approach was to set the cookie manually inn the client but was wondering if there was a easier way since i works on local host. But i have read that this type of approach work only on same site i.e between subdomains. Can you confirm this?

@dougwilson
Copy link
Contributor

I totally understand, cookies can be complex with various security segments in the web browsers. I have to admit, my front end knowledge is rusty, especially with all the changes the web browsers keep making. I'm not sure how to answer your question, I am sorry.

@joewagner
Copy link
Member

@adamkb33 If you are using two different domains for production, i.e. one for your api and one to serve your html+css+js, then those two domains are not going to have access to the same cookies without setting up CORS. Even then browsers don't always behave the same so things can be tricky.
Here is a good SO question+answer that might help you find a solution: https://stackoverflow.com/questions/46288437/set-cookies-for-cross-origin-requests
FWIW IMO the simplest and most likely to be secure is setting up a reverse proxy so everything lives under the same domain.

@AtilMohAmine
Copy link

Based on the information provided, it appears that you've correctly identified the potential need for adjusting the SameSite attribute to 'None' in your cookie options to facilitate cross-site requests. This adjustment is crucial for enabling proper functionality, particularly in production environments.

response.cookie('jwt', accessToken, {
      maxAge: 15 * 60 * 1000,
      httpOnly: true,
      secure: env === 'production' ? true : false,
      sameSite: 'None'
    });

To delve deeper into this topic, please refer to the MDN documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants