I create a cookie from server side and I want to send it to client side with axios call and set it dynamic
this is server side API and works fine.
router.post('/login', async (req, res) => {
const {email, password} = req.body;
const checkEmail = await User.findOne({where: {email: email}});
// check email is correct
if (checkEmail) {
const checkPassword = await User.findOne({where: {email: checkEmail.email, password: password}})
// Check if is password is correct
if (checkPassword) {
const user = checkPassword;
const token = jwt.sign({id: user.id, email: user.email}, 'theSecretKey', {expiresIn: '48h'});
res.cookie('jwt', token, {maxAge: 3600000, httpOnly: true}); // 1 hour
return res.json({user});
}
return res.json({message: 'password is wrong'});
}
res.json({message: 'Email or password is wrong'});
});
And my problem with client side I want when I call this Api make a cookies direct to my client side from server side.
const handleSubmit = async (event) => {
event.preventDefault();
const data = {
email: email,
password: password
}
console.log(data);
// call axios
await axios.post('/login', data, {
headers: {
'Content-Type': 'application/json',
}
}).then((res) => {
console.log(res.data);
if (res.data.user) {
console.log(res.data.user);
}
});
};
also I add withCredentials in axios
axios.create({
baseURL: 'http://localhost:5000',
withCredentials: true,
credentials: 'include',
});