Passport.js使用failureRedirect呈现错误401

时间:2017-11-29 17:29:11

标签: node.js express authentication mongoose passport.js

我正在使用passport.js本地策略进行注册和登录,当我尝试向其传递数据的任何人提供正确的数据并在任何类型的错误数据时移动到successRedirect /home时没有闪烁错误登录案例中的未注册数据或注册案例中的重复数据,它会转到failuerRedirect /login,而是通过文本Unauthorized

来查看其显示的erorr 401

以下是我的代码

Passport.js文件

var passport = require('passport');
var User = require('../models/user');
var LocalStrategy = require('passport-local').Strategy;

passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    User.findById(id, function (err, user) {
        done(err, user);
    });
});

passport.use('User.signup', new LocalStrategy({
    usernameField : 'email',
    passwordField: 'password',
    passReqToCallback : true
}, function (req, email, password, done) {
        var FirstName = req.body.Firstname;
        var LastName = req.body.Lastname;
        var email = req.body.email;
        var RollNo = req.body.rollno;
        var Gender = req.body.Gender;
        var password = req.body.password;
    User.findOne({'email': email}, function (err, user) {
        if (err) {
            return done(err);
        }
        if (user) {
            return done(null, false, {signupMessage: 'Email is already in use.'});
        }
        var newUser = new User();
        newUser.First_Name = FirstName;
        newUser.Last_Name = LastName;
        newUser.email = email;
        newUser.Roll_No = RollNo;
        newUser.Gender = Gender;
        newUser.password = newUser.encryptPassword(password);
        newUser.save(function(err, result) {
           if (err) {
               return done(err);
           }
           return done(null, newUser);
        });
    });
}));

passport.use('user.login' , new LocalStrategy({
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true
}, function (req , email  , password , done ){
    User.findOne({'email' : email} ,function(err , user) {
        if(err){
                    return done(err)
                }
        if(!user){
                    return done(null , false )
                }
        if(!user.validPassword(password)){
                    return done(null , false )
                }

        return done(null , user);
    });
}));

用户模型

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var bcrypt = require('bcrypt-nodejs');

const UserSchema = new Schema({
  First_Name : { type : String , required : true  , minLength: 2 },
  Last_Name : { type : String , required : true  , minLength : 2},
  email : { type : Schema.Types.Mixed, unique : true, required : true, dropDups: true },
  Roll_No : { type : Schema.Types.Mixed, unique : true, required : true, dropDups: true },
  Gender :  String ,
  password : { type : Schema.Types.Mixed, required : true}
},{collection : 'Users'});

UserSchema.methods.encryptPassword = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null);
};

UserSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.password);
}
var User = mongoose.model('User' , UserSchema); 

module.exports = User;

和路线

/* Get Login */

router.get('/login', function(req, res, next) {
  res.render('login');
});

/* Post Login */

router.post('/login', passport.authenticate('user.login', {
  successRedirect : '/home',
  failuerRedirect : '/login',
  failuerFlash : true
}));


/* Get SignUp */

router.get('/signup', function(req, res, next) {
  res.render('signup');
});

/* regester New user */

router.post('/signup', passport.authenticate('User.signup' , {
  successRedirect : '/home',
  failuerRedirect : '/signup',
  failuerFlash: true
}));

2 个答案:

答案 0 :(得分:1)

您在路线中拼错了一些字,请查看以下代码:

/* Get Login */

router.get('/login', function(req, res, next) {
  res.render('login');
});

/* Post Login */

router.post('/login', passport.authenticate('user.login', {
  successRedirect : '/home',
  failureRedirect : '/login',
  failureFlash : true
}));


/* Get SignUp */

router.get('/signup', function(req, res, next) {
  res.render('signup');
});

/* register New user */

router.post('/signup', passport.authenticate('User.signup' , {
  successRedirect : '/home',
  failureRedirect : '/signup',
  failureFlash: true
}));

答案 1 :(得分:0)

最近我在护照本地工作我已经过了git url,我在代码中找不到错误 https://github.com/sourabhkum/expressapp