2

Here is client side code that sends a fetch request to the /loginCheck route.

console.log("In user login checking function");
 readAllData('loginDetail')
 .then(function(loginDetails){
    console.log(JSON.stringify(loginDetails[0])); 
    return fetch('/loginCheck', {
      method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: JSON.stringify(loginDetails[0])
    })
 .then(function(res){
   console.log(res.status);
 })
 .catch(function(err){
   console.log(err);
 })   
});

Here is the server side code that checks if the user is logged in and if he is i want to redirect to the main.html page.

app.post('/loginCheck', (req, res)=>{
  const userName=req.body.nickName;
  console.log(userName);
     Detail.findOne({nickName:userName}, function(err, docs){
      if(err){
        console.log(err);
      }else{
        console.log("Inside else");
        if(!docs){
            res.redirect('/');
        }else{
          isLogged=true;
          console.log(isLogged);
          console.log("Redirecting to the main page");
          res.redirect('/main.html');
        }
      }
    })
});

The log statements are getting executed but the redirecting is not working.Could Anyone please point whats going wrong?

Prajval M
  • 2,298
  • 11
  • 32
  • The redirect which you're doing is on the server side. Hence your response to the client will be that of / or /main.html route (if exists) on the server. – Prajval M Oct 01 '20 at 08:47
  • Does this answer your question? [How to redirect the user to another page after login using JavaScript Fetch API?](https://stackoverflow.com/questions/75184430/how-to-redirect-the-user-to-another-page-after-login-using-javascript-fetch-api) – Chris Jun 10 '23 at 05:51

1 Answers1

3

You must do redirection code in client side not in server side.

So here is the correct code

console.log("In user login checking function");
 readAllData('loginDetail')
 .then(function(loginDetails){
    console.log(JSON.stringify(loginDetails[0])); 
    return fetch('/loginCheck', {
      method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    },
    body: JSON.stringify(loginDetails[0])
    })
 .then(function(res){
   console.log(res.status);
   if (res.status == true){
        location.href = '/main.html';

   } else {
        location.href = '/';
   }
 })
 .catch(function(err){
   console.log(err);
 })
  

});

//Here is the server side code that checks if the user is logged in and if he is i want to redirect to the main.html page.

app.post('/loginCheck', (req, res)=>{
  const userName=req.body.nickName;
  console.log(userName);
     Detail.findOne({nickName:userName}, function(err, docs){
      if(err){
        res.json({ status: false}); 
        console.log(err);
      }else{
        console.log("Inside else");
        if(!docs){
            res.json({ status: false});   
        }else{
          let isLogged=true;
          console.log(isLogged);
          res.json({ status: true});   
        }
      }
    })
  
});

  • Answer seems right. Just one more point to be noted is location.href=.... part of yours will work only if front end is on live server – Prajval M Oct 01 '20 at 08:49
  • It doesn't seem to work.The page gets served continously. – Aditya Apte Oct 01 '20 at 16:27
  • Have you checked response ? Have you debugged results on client side what you are getting ? A page couldn't served continuously untill and unless you call login api in page load instead of login click event. – Hardik Sukhadiya Oct 02 '20 at 09:10