passport / discuss

A forum for discussing topics related to the usage of Passport.js.
1 stars 0 forks source link

saving passport session doesn't work when I change the url code to dotenv (or hardcodeded). #55

Open lunacer opened 3 years ago

lunacer commented 3 years ago

Using passport local strategy. my front-end code is like below, and it works fine in localhost with a proxy setting. Axios.post(/api/users/login, loginData)

But when I change it to environment variable (preparing deploy), the passport object doesn't show in the session. Environment variable with dotenv: Axios.post(${process.env.REACT_APP_API_URI}/api/users/login, loginData) or hardcode to local host: Axios.post(http://localhost:8080/api/users/login, loginData)

If I change it back it works fine again. Serializer was called, user-id returns to the frontend and I can save it in local storage. But no passport data in session, hence, the deserializer doesn't work as well.

It seems the issue occurs when I don't use proxy settings. I'm using "http-proxy-middleware" for localhost for a development environment, it works and when the request doesn't pass the middleware it happens. I tried to add this in Axios header, { withCredentials: true }

and server: ` app.use( cors({ // origin: "*", origin: [ "http://localhost:8000", "https://powerful-badlands-37777.herokuapp.com/", ], credentials: true, }) );

app.use(function (req, res, next) { res.header( "Access-Control-Allow-Origin", "http://localhost:8000", "https://powerful-badlands-37777.herokuapp.com/" ); // update to match the domain you will make the request from res.header( "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept" ); next(); });`

and cookie setting also added this(because I'm not sure the cause of the issue) cookie: { secure: true, maxAge: 24 * 60 * 60 * 1000, // 24 hours sameSite: "None", },

It would be a really big help if you can advise me where to look to fix it. I'm lost and in hurry... Thanks.