I am building my web app and just finished the login controllers where once user put valid credentials, a JWT is created and set in the headers authorization then the client side will do the redirection with location.replace
. however it does not work at all indeed once I put the username and password I am not redirect and nothing is shown on the network section of my brother but if I only do a post request I can see it on the network’s browser tab.
The other solution was with axios
in client side, first do a post request then a get request but it only works on the network tab but not redirecting to the dashboard page.
I’ve read many thread but it seems that none of them have an answer for that or I may have been missing something
Here example of my code:
First my auth middleware:
const auth = async (req,res) => {
const {username , password} = req.body
if (!username || !password) {
return res.status(404).json({
message: `Veuilliez entrez un mot de passe et/ou un nom d'utilisateur`
})
}
const user = await User.findOne({ username });
if (!user) {
return res.status(404).json({
message : `Les informatios entrées ne sont pas correctes`
})
}
const isSamePassword = await user.comparePassword(password);
if (!isSamePassword) {
return res.status(404).json({
message : `Les informatios entrées ne sont pas correctes`
})
}
const token = user.createJWT();
res.header('authorization', 'Bearer '.concat(token)).json({
username : username,
token : token
})
}
my client side js:
submitForm.addEventListener('submit', async function(e) {
e.preventDefault();
try {
const response = await axios.post('/user/login',{
username: username.value,
password: password.value
}).then(
() => {
location.replace('/transferts')
}
)
} catch (error) {
console.log(error);
}
})
Can anyone help me please