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
Comments
Hey @Code-Crash, might this be of help? |
@cloudhary , Thanks for the help. let me test it and will get back to you in some time. |
@Code-Crash you are right , just noticed the same and its working with postman ... trying what @cloudhary introduced |
@Code-Crash you need to pass example: axios.get('/test', {withCredentials: true}).then(console.log).catch(console.error); otherwise |
@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. |
I figured it out i think your issue will have the sample problem too .. 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
}));
}; |
@ahmedyounes Thanks for the hint, maybe I will try it once and will post here if the solutions work. Thanks again! 👍 |
Did you solve it? I'm having same issue |
@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. |
I've solved it. Thank you so much for your kind gesture <3
…On Fri, 26 Aug 2022 at 11:04, Pravin Tiwari ***@***.***> wrote:
@HazratAliii <https://github.com/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.
—
Reply to this email directly, view it on GitHub
<#119 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AL2SRCIGLM5XGDA7JUGZMVLV3BF7FANCNFSM4G75CCDQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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.
The text was updated successfully, but these errors were encountered: