Reputation: 9095
I have a written a service to check whether the user is already logged in or not, but here i am not able to run the code in localhost. Why i am not able to run the below code in localhost.
The redirect is not working, to redirect to the home url it should be res.redirect("/") isn't ?
function checkUser(req,res){
// checking cookie exists
let authenticatedUser = checkCookieExist(req.headers.cookie, "username");
if(authenticatedUser){
res.status(200).redirect("/home")
}else{
// redirect to login page, once its success redirect it to root url
res.status(200).redirect("/")
}
}
Upvotes: 0
Views: 1201
Reputation: 17566
You set invalid response code. In order for the redirect to work, it has to be one of the 3XX
. Usually 302
for a temporary redirect, eg.:
res.status(302).redirect("/")
This is done by default, so simple would do:
res.redirect("/")
Upvotes: 3