我正在设置一个带有节点,express,passport并以mysql作为数据库的续集的后端服务器。我尝试了许多教程中的许多代码,但我仍然在同一地方。在终端中,我没有收到任何错误,但是successRedirect方法不起作用。
我的password.js文件看起来不错,但是我最近开始使用护照库,所以我无法发现任何错误。
let LocalStrategy = require('passport-local').Strategy;
let {user}=require('../src/sequelize');
let bCrypt = require('bcrypt-nodejs');
let User=user;
module.exports = function(passport) {
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('local-signup', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
},
function(req, username, password, done) {
var generateHash = function(password) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null);
};
user.findOne({
where: {
username: username
}
}).then(function(user, err) {
console.log('I entered',user);
console.log('I entered',err);
if(err) {
console.log(err);
return done(null, false);
}
if(user == null) {
User.create({
username: username,
lastname:req.body.lastname,
name : req.body.name,
password: generateHash(password)
}).then(function(usered) {
return done(null, usered);
}).catch(function(err) {
return done(null, err);
});
}
if(user){
return done(null, false, {message: 'That email is already taken' });
}
})
}));
}
还有我的userController文件
const userController=require('../controllers/userController');
const express = require('express');
const router = express.Router();
const passport=require('passport');
router.get('/signup', userController.signup);
router.get('/signin', userController.signin);
router.post('/signup', passport.authenticate('local-signup', {
successRedirect: '/dashboard',
failureRedirect: '/signup',
flashMessage: true
}
))
router.get('/dashboard',userController.dashboard);
module.exports = router;