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

Not working with axios (chrome) but working with Postman #119

Closed
Code-Crash opened this issue Mar 20, 2019 · 10 comments
Closed

Not working with axios (chrome) but working with Postman #119

Code-Crash opened this issue Mar 20, 2019 · 10 comments
Labels

Comments

@Code-Crash
Copy link

Hi,

when calling login API with the postman, on postman cookie its getting save and also getting removed when calling login API.

but the same code is not working with axios (in react on chrome [MacBook]).

my client server (with react) is running localhost:3000 and API server is running on localhost:1337.

I don't know what I'm doing wrong or it's a problem with a different domain.

any help will be appreciated.
Thanks.

@cloudhary
Copy link

Hey @Code-Crash, might this be of help?

@Code-Crash
Copy link
Author

Code-Crash commented Mar 21, 2019

@cloudhary , Thanks for the help. let me test it and will get back to you in some time.

@ahmedyounes
Copy link

@Code-Crash you are right , just noticed the same and its working with postman ... trying what @cloudhary introduced

@unlucio
Copy link

unlucio commented Apr 2, 2019

@Code-Crash you need to pass {withCredentials: true} to axios:
axios[method](url, {withCredentials: true})

example:

axios.get('/test', {withCredentials: true}).then(console.log).catch(console.error);

otherwise axios is not going to carry the cookies with the request.

@Code-Crash
Copy link
Author

@unlucio , Yes, I tried that too, but I was getting CORS issue, and I have enabled the CORS on the server side also all the required headers, not sure what I was doing wrong.

before posting the query over here, I already did Google and Stack Overflow and tried a bunch of approaches, but no luck.

I didn't had much time, so I switch back to my own approach, passing the session token in headers from local storage and validating the same in middleware.

Thanks for the help.

@ahmedyounes
Copy link

ahmedyounes commented Apr 4, 2019

I figured it out i think your issue will have the sample problem too ..
in proxy configuration make sure you are matching any path
with double ** not only *

const proxy = require("http-proxy-middleware");
module.exports = function(app) {
  app.use(proxy("/api/**", { // https://github.com/chimurai/http-proxy-middleware
    target: "http://localhost:5000",
    secure: false
  }));
};

@Code-Crash
Copy link
Author

@ahmedyounes Thanks for the hint, maybe I will try it once and will post here if the solutions work.

Thanks again! 👍

@HazratAliii
Copy link

Did you solve it? I'm having same issue

@Code-Crash
Copy link
Author

@HazratAliii Yes, I was able to solve the issue, when we are talking about setting and getting the cookie, cors play an important role, please see if you are adding all the required details properly, as of now I'm not aware about your setup but if your server setup is in node with express, please see the below sample code:

/**
 * This method will help to set the cookie in response
 * @param {HttpResponseObject} res This is http response object on which we will apply the cookie
 * @param {string} token token to be applied on cookie
 * @param {string} domain for which domain we want to set the cookie
 */
const SetAccessTokenCookieInResponse = (res, token, domain) => {
  // Set the cookie in the response object
  logger.info('SetAccessTokenCookieInResponse - domain: ' + domain);
  const options = {
    maxAge: process.env.AUTH_SECRET_TOKEN_LIFE * 1000, // AUTH_SECRET_TOKEN_LIFE is in seconds, convert seconds to ms
    httpOnly: true,
    path: '/',
    secure: (process.env.NODE_LOCAL_ENV && process.env.NODE_LOCAL_ENV === 'local') ? false : true, // NOTE: while development on local, we need to pass secure:false and for any other env, it should be true
    domain: (process.env.NODE_LOCAL_ENV && process.env.NODE_LOCAL_ENV === 'local') ? 'localhost' : (process.env.AUTH_COOKIE_DOMAIN || domain), // domain name should be same, we can also use sub domain which is set in config
  };

  if (!process.env.NODE_LOCAL_ENV || process.env.NODE_LOCAL_ENV !== 'local') {
    options.sameSite = 'none';
  }

  res.cookie('your-cookie-name', token, options);
};
  
  

Also, make sure your headers are properly set as per CORS policy.

@HazratAliii
Copy link

HazratAliii commented Aug 26, 2022 via email

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

No branches or pull requests

6 participants