Подскажите как можно сделать так, чтобы система автоматом определяла что вошел админ и направляла на отдельную страницу? Авторизацию делал на node.js passport.js.
passport.js
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
connection.query("SELECT * FROM users WHERE id = ? ", id, function(err, rows){
done(err, rows[0]);
});
});
passport.use('local-login', new LocalStrategy({
usernameField : 'username',
passwordField : 'password',
passReqToCallback : true
},
function(req, username, password, done) {
connection.query("SELECT * FROM users WHERE username = ?", username, function(err, rows) {
if(err) return done(err);
if(!rows.length) {return done(null, false, req.flash('loginMessage', 'No user found.'));}
if(!bcrypt.compareSync(password, rows[0].passwd))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));
return done(null, rows[0]);
});
})
);
};
router.js
module.exports = function(app, passport) {
app.get('/', function(req, res, next) {
res.render('index', {message: req.flash('message')});
});
app.post('/login', passport.authenticate('local-login', {
successRedirect : 'home', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}),
function(req, res) {
console.log("hello");
if (req.body.remember) {
req.session.cookie.maxAge = 1000 * 60 * 3;
} else {
req.session.cookie.expires = false;
}
res.redirect('/');
});
app.get('/home', isLoggedIn, function(req, res) {
res.render('home', {
user : req.user // get the user out of session and pass to template
});
});
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
};
}