#Can't login, cookie not getting set

36 messages · Page 1 of 1 (latest)

shy terrace
#

Hello, I have a problem with my railway deployement, my front has been successfully deployed on netlify and my back on railway, both are successfully linked and I can create an account, but my problem is that I can't login myself, I have a problem with my cookies, I suppose.
I had this error message :
Access to XMLHttpRequest at 'https://[my-back-url].adaptable.app/jwtid' from origin 'https://[my-front-url].netlify.app/' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
until I put this cors option :

app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content, Accept, Content-Type, Authorization"
);
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE, PATCH, OPTIONS"
);
next();
});

because just puting my front url didn't work, then I had to erase all my withcredentials : true from my front (because I had an error of incompatibility of "Access-Control-Allow-Origin : *" and credentials on true)

That was ok, no error message but I stilled can't login, the cookie doesn't appear...)

I've read that a custom domain could make it work, but I can't buy a domain for that (src : #1048780024691556372 message)

Can anyone help me ? Do anyone have already had this problem or have an idea to resolve it ?

(I'm french, so forgive my bad english 🤦‍♀️ )

Thanks for reading me and have a nice evening 😊 !

Discord

Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.

silent thistleBOT
#

Project ID: N/A

#

It appears that KonstTheoGR is having an issue with logging in to Railway, while JaLePo is having an issue with cookies not getting set in the browser when using Railway.

shy terrace
#

I forgot to mention that it's a MERN project and that I've already tried that :
res.cookie("jwt", token, { httpOnly: true, maxAge, sameSite: 'none', secure : true, domain : "https://[front-url].netlify.app" });
no result squidwtf

limpid phoenix
#

very odd, i cant reproduce this using the echo framework for Go, but i can reproduce this using the express framework for nodejs

#

i will look into it

shy terrace
limpid phoenix
#

it is a code issue though just fyi

shy terrace
limpid phoenix
#

whats MP

shy terrace
#

private message

#

PM, sorry (in french it's inverted)

limpid phoenix
#

just hold on a sec

#

express is picky, remove the domain option from res.cookie function and try again

#

from my minimal test, cookies dont get set when a domain is specified in the options, remove the domain option and cookie gets set

shy terrace
limpid phoenix
#

this might have something to do with railway not providing a host header so express cant verify the domain and fails to set a cookie when a domain is set in the options, but thats just speculation on my part

#

jwt cookie does get set, thats using express without specifying domain

shy terrace
# limpid phoenix this might have something to do with railway not providing a host header so expr...

So I should remove all that code :

app.use((req, res, next) => { res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content, Accept, Content-Type, Authorization" ); //autorisation d'ajouter les headers mentionnés aux requêtes envoyées vers notre API (Origin , X-Requested-With , etc.) ; res.setHeader("Access-Control-Allow-Credentials", "true"); res.setHeader( "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, PATCH, OPTIONS" ); next(); });
?

limpid phoenix
#

i didnt say that

#

but you would need to set some of that stuff correctly, "" isn't a valid header

shy terrace
limpid phoenix
#

Use code formatting next time

shy terrace
limpid phoenix
#

Have you removed the domain setting from the options

shy terrace
#

Ah, it worked, sorry !

shy terrace
limpid phoenix
#

Okay now just make sure you setup the cors headers correctly, using a wildcard is not something you want to do in production

shy terrace
limpid phoenix
#

That would be a question for some tutorials

shy terrace
limpid phoenix
#

You'll get it

shy terrace
limpid phoenix
#

I've seen someone else have success with that, and for a much more complex problem, so give it a try, but as it stands this isnt really pertaining to railway anymore so good luck! 🙂