我遇到了一个问题,试图调试我的PassportJS授权无法正常工作的原因。我试图在每个if / else语句中添加console.log
,但我的终端中没有出现任何内容。我知道存在问题的唯一原因是因为我正在为注册用户使用正确的凭据,但我在提交表单后获得了失败重定向。这可能是因为我使用的参数不同于'用户名'而且我没有正确设置?我是否需要传递表单中的值?
模型索引:
var Sequelize = require('sequelize');
var path = require('path');
var config = require(path.resolve(__dirname, '..', '..','./config/config.js'));
var sequelize = new Sequelize(config.database, config.username, config.password, {
host:'localhost',
port:'3306',
dialect: 'mysql'
});
sequelize.authenticate().then(function(err) {
if (!!err) {
console.log('Unable to connect to the database:', err)
} else {
console.log('Connection has been established successfully.')
}
});
var db = {}
db.User = sequelize.import(__dirname + "/user");
db.sequelize = sequelize;
db.Sequelize = Sequelize;
sequelize.sync();
module.exports = db;
user.js的:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('users', {
annotation_id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
firstName: {
type: DataTypes.STRING,
field: 'first_name'
},
lastName: {
type: DataTypes.STRING,
field: 'last_name'
},
email: DataTypes.STRING,
password: DataTypes.STRING,
}, {
freezeTableName: true,
instanceMethods: {
generateHash: function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
},
validPassword: function(password) {
return bcrypt.compareSync(password, this.password);
},
}
});
return User;
}
路线:
var express = require('express');
var siteRoutes = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var models = require('../models/db-index');
/*==== Passport Configuration ====*/
// Serialize sessions
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
db.User.find({where: {id: id}}).success(function(user){
done(null, user);
}).error(function(err){
done(err, null);
});
});
passport.use(new LocalStrategy({
passReqToCallback : true,
usernameField: 'email',
passwordField : 'password'
}, function(username, password, done) {
//Find user by email
User.findOne({ username: username }, function(err, user) {
//If there is an error, return done
if (err) { return done(err); }
//If user does not exist, log error and redirect
if (!user) {
console.log('No email')
return done(null, false, { message: 'Incorrect username.' });
}
//If user exists, but wrong password
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
console.log('Password fail');
}
//If all credentials match, return user
return done(null, user);
console.log('Successful login');
});
}
));
/*==== Index ====*/
siteRoutes.get('/', function(req, res){
res.render('pages/index.hbs');
});
/*==== Login ====*/
siteRoutes.route('/login')
.get(function(req, res){
res.render('pages/login.hbs');
})
.post(passport.authenticate('local', {
successRedirect: '/app',
failureRedirect: '/',
failureFlash: true
}));
siteRoutes.route('/sign-up')
.get(function(req, res){
res.render('pages/sign-up.hbs');
})
.post(function(req, res){
models.User.create({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: req.body.password
}).then(function() {
res.redirect('/');
}).catch(function(error){
res.send(error);
})
});
module.exports = siteRoutes;
登录表格:
<!DOCTYPE html>
<head>
{{> head}}
</head>
<body>
{{> navigation}}
<div class="container">
<div class="col-md-6 col-md-offset-3">
<form action="/login" method="post">
<label for="login-username">Username</label>
<input type="text" class="form-control" id="login-username" name="username" placeholder="username or email">
<br />
<label for="login-password">Password</label>
<input type="password" class="form-control" id="login-password" name="password">
<div class="login-buttons">
<button type="submit">Login</button>
</div>
</form>
<a href="/sign-up">Don't have an account? Then register here!</a>
<br />
<a href="#">Forgot your password?</a>
</div>
</div>
</body>
答案 0 :(得分:0)
您将电子邮件指定为登录属性,但使用了“用户名”。
User.findOne({ username: username }
正如我所看到的,用户模型中没有名为username的字段。
我认为,您应该将用户名更改为电子邮件。
User.findOne({ email: username }