Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var requireLogin = function (req, res, next) {
- if (req.session.user) {
- next();
- } else {
- res.redirect('/');
- }
- }
- app.post('/login', requireLogin, routes.login);
- exports.login = function (req, res) {
- var query = {username:req.body.username, password:req.body.password};
- User.find(query, function (err, data) {
- if (data) {
- req.session.user = data;
- console.log(data);
- res.redirect('/home');
- } else {
- console.log(err);
- res.redirect('/');
- }
- });
- };
- app.post('/login', requireLogin, routes.login);
- exports.login = function (req, res) {
- var query = {username:req.body.username, password:req.body.password};
- User.findOne(query, function (err, data) {
- if (data && !err) {
- req.session.user = data;
- console.log(data);
- res.redirect('/home');
- } else {
- console.log(err);
- res.redirect('/');
- }
- });
- };
- User.findOne(query, function (err, data) {
- if (err) {
- // the username/password could be valid (or not),
- // but there's no way to tell because there was some server error
- console.log(err);
- res.redirect('/');
- } else if (data) {
- // a user was found that matched the query
- req.session.user = data;
- console.log(data);
- res.redirect('/home');
- } else {
- // there is no User that matches the query
- res.redirect('/');
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement