Hi everyone, thanks for reading this i'm a self taught developer and I've already spent 5 days straight to make it work alone but really couldn't 😢
I use Passport.js for session-based authentication which requires cookies to work. My server and client are on different domain (client : https://min-workout-timer.onrender.com/
server: https://workout-timer-server-production.up.railway.app/)
When I tested it locally, both works fine. 1.Passport.js serialize and deserialize properly
2. Server res header has 'Set-Cookie' on the initial login and Client has 'cookie' in the subsequent request.
However when I built and deployed both of them,
- Passport.js only serialize but doesn't deserialize
- There's no cookie at all in the req and res from both sides.
Here are some configuration from my server and client side
index.ts (server side)
app.use(
cors({
origin: 'https://min-workout-timer.onrender.com/' || 'http://127.0.0.1:5173',
credentials: true,
})
);
const storeOptions = {
.....
};
const sessionStore = new MySQLStore(storeOptions);
app.use(
session({
secret: process.env.MY_SESSION_SECRET,
store: sessionStore,
resave: false,
saveUninitialized: false,
cookie: {
secure: true,
sameSite: 'none',
httpOnly: true,
},
})
);
signin.ts (client side)
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
try {
const res = await axios.post<FormData>(
'https://localhost:8080/api/users/signin',
{
email: formData?.email,
password: formData?.password,
},
{ withCredentials: true }
);
if (res.status === 200) {
localStorage.setItem('user', JSON.stringify(res.data));
navigate('/plan');
}
} catch (error: any) {
if (error.response.status === 403) alert(error.response.data);
else alert('Server failed to respond');
}
}
At this point idek if cross site cookie still work😖