0

How I can use condition in my express.js server. If user login I want to change roots and side server rendering my react app. This condition dont work if(!isLoggin) if user login I change it to true, so this should render my index with react. Wher is problem ?

 if(typeof(isLoggin)=='undefined'){
        var isLoggin = false;
    }
    //roots

    if(!isLoggin){
        //index
        app.get('/', function(req, res){
            res.render("indexNotLogin",{title:''});
        })
        //funkcje
        app.get('/funkcje', function(req, res){
            res.render("funkcje",{title:'Funkcje'});
        })

        //zaloguj
        app.get('/zaloguj', function(req, res){
            res.render("zaloguj",{title:'Zaloguj się'});
        })
        //zaloguj
        app.post('/trylogin', function(req, res){
            var username = req.body.name;
            var password = req.body.password;
            connection.query("SELECT * FROM user WHERE username='"+username+"' AND pass='"+password+"'", function(error, rows,fields){
                //callback
                if(!!error){
                    console.log('error in query');
                }else{
                    if(rows.length){
                        res.send('user zalogowany')
                        isLoggin=true;
                    }else{
                        res.send('user nie istnieje')
                    }
                }
            })

        })
    }else{
        console.log("to jest true: "+isLoggin);
        app.get('/', function(req, res){
            res.render("index",{title:'Zaloguj sie'});
        })
    }

@edit /zaloguj, /funkcje this is my static roots

Paweł Baca
  • 814
  • 2
  • 15
  • 28
  • 1
    Watch out SQL INJECTION ```connection.query("SELECT * FROM user WHERE username='"+username+"' AND pass='"+password+"'",``` – Risto Novik Nov 28 '17 at 12:53
  • 1
    You don't need `isLoggin` flag and don't use some conditional adding/removing routes it adds huge complexity. Better use sessions https://stackoverflow.com/questions/7990890/how-to-implement-login-auth-in-node-js – Risto Novik Nov 28 '17 at 12:56
  • I shound add Limit 1 on end request ? – Paweł Baca Nov 28 '17 at 12:56

2 Answers2

1

At first, implement login with session, not a flag.

app.post('/trylogin', function(req, res){
  var username = req.body.name;
  var password = req.body.password;
  connection.query("SELECT * FROM user WHERE username='"+username+"' AND pass='"+password+"'", function(error, rows,fields){
    if(!!error){
      console.log('error in query');
    }else{
      if(rows.length){
        res.send('user zalogowany')
        req.session.user_id = put_user_id_here
      }else{
        res.send('user nie istnieje')
      }
    }
  })
})

If you want to check if the user is logged in or not in order to restrict an access, it is better to implement your own Express middleware (http://expressjs.com/en/guide/using-middleware.html#middleware.router). With the authentication middleware like below, you don't have to add a condition block which wraps route definitions.

app.use(function(req, res, next) {
  if (req.session.user_id) {
    next();
  } else {
    res.status(401).send('Unauthorized')
  }
});
IzumiSy
  • 1,508
  • 9
  • 17
0

Like this ?

function checkAuth(req, res, next) {
    console.log('Jakies id: '+req.session.user_id);

  if (!req.session.user_id) {
      if(req.route.path=='/'){
          res.render("indexNotLogin",{title:''});
      }else{
        res.send('You are not authorized to view this page');
    }
  } else {
    next();
  }
}

My root to after success login:

//index
app.get('/', checkAuth, function(req, res){
    res.render("index",{title:''});
})
Paweł Baca
  • 814
  • 2
  • 15
  • 28