我已经尝试了一切。我仔细检查了所有回答问题的答案。我绝望了。
我在我的网站上使用了护照模块,而且successRedirect无法正常工作,而failureRedirect完全符合它的预期。 这是 users.js ,它会进行路由。
'use strict';
const passport = require('passport');
const User = require('../models/user');
module.exports = function(_, passport){
return {
SetRouting: function(router){
console.log('got it');
router.get('/', this.indexPage);
router.get('/signup', this.getSignUp);
router.get('/home', this.homePage);
router.post('/signup', this.postSignUp);
},
indexPage: function(req, res){
console.log('got index page');
return res.render('index');
},
getSignUp: function(req, res){
console.log('got signup page');
return res.render('signup');
},
homePage: function(req, res){
console.log('got home page');
return res.render('home');
},
postSignUp: passport.authenticate('local.signup', {
successRedirect: '/home',
failureRedirect: '/signup',
failureFlash: true
}),
}
这是 passport-local.js ,我在那里进行本地策略函数调用:
'use strict';
const passport = require('passport');
const User = require('../models/user');
const LocalStrategy = require('passport-local').Strategy;
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
passport.use('local.signup', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, (req, email, password, done) => {
User.findOne({'email': email}, (err, user) => {
if(err){
return done(err);
}
if(user){
return done(null, false, req.flash('error', 'User with email already exist'));
}
const newUser = new User();
newUser.username = req.body.username;
newUser.fullname = req.body.username;
newUser.email = req.body.email;
newUser.password = newUser.encryptPassword(req.body.password);
newUser.save((err) => {
done(null, newUser);
});
});
}));
我非常高兴你的帮助!
答案 0 :(得分:0)
您的策略名为'local.signup'
。策略名称不能包含句点。
在很多地方,Passport都将策略名称用作对象键和变量标识符。例如,Passport's Authenticator以策略名称作为键来构造策略的对象,并在以后引用:
Authenticator.prototype.use = function(name, strategy) {
...
this._strategies[name] = strategy;
return this;
};
要使其正常工作,name
必须是有效的Javascript标识符,表示没有句点。