Passport.js 使用 failureRedirect 呈现错误 401
Posted
技术标签:
【中文标题】Passport.js 使用 failureRedirect 呈现错误 401【英文标题】:Passport.js Render error 401 with failureRedirect 【发布时间】:2018-05-13 11:54:07 【问题描述】:当我尝试向传递数据的任何人提供正确的数据并移动到successRedirect/home
同时处理任何类型的错误数据或未注册数据时,我正在使用passport.js本地策略进行注册和登录,而不会出现错误在登录案例或注册案例中的重复数据中,它会转到 failuerRedirect /login
,而是使用文本 Unauthorized
显示错误 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
));
【问题讨论】:
【参考方案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
));
【讨论】:
【参考方案2】:最近我在本地护照上工作,我已经过去了 git url,我在你的代码中找不到错误 https://github.com/sourabhkum/expressapp
【讨论】:
以上是关于Passport.js 使用 failureRedirect 呈现错误 401的主要内容,如果未能解决你的问题,请参考以下文章
为啥除了 passport.js 还要使用 cookie-session?
如何在 passport-facebook / Passport.js 中捕获 FacebookAuthorizationError?