node.js - How do I write a conditional for express-sessions login/logout in my main handlebars layout? -
i have login/logout routes written in controllers.
//login form app.get('/login', function (req, res) { res.render('../views/signup-signin/login'); }); //login post app.post('/login', function (req, res) { user.find(req.body.username, function (user) { bcrypt.compare(req.body.password, user.password, function (err, result) { if (result) { req.session.currentuser = user.id; res.redirect('/'); } else { res.redirect('/login'); } }); }); }); //logout app.delete('/logout', function (req, res) { req.session.currentuser = null; res.redirect('/'); });
what i'm having trouble being able write if/else statement in 'main.handlebars' layout renders sign up/login when req.session.currentuser = null, , logout when req.session.currentuser = user.id
<div id="signin_up"> <a href="/signup"><button>sign up</button></a> <a href="/login"><button>login</button></a> </div> <form action="/logout" method="post"> <input type="hidden" name="_method" value="delete"> <input type="submit" value="logout"> </form>
i can't figure out how access sessions id , write in script tag in 'main.handlebars' file.
can me feature working?
thanks ahead of time!
Comments
Post a Comment