我正在创建一个小型演示应用程序,以学习passport.js的用户身份验证,续集和表达。当我点击/ register端点时,它会创建一个用户帐户。与如果我点击/ logIn端点相同,如果我给它正确的凭据,它将返回正确的用户。
我遇到的问题是,启动服务器并按下/ register或/ logIns端点后,服务器将停止响应所有GET和POST请求,直到重新启动服务器为止。
这是我的index.js代码:
var express = require('express')
var bodyParser = require('body-parser')
var Sequelize = require('sequelize')
var session = require('express-session')
var passport = require('passport')
var bCrypt = require('bcrypt')
var LocalStrategy = require('passport-local').Strategy
var flash = require('connect-flash')
var morgan = require('morgan')
var app = express()
app.use(express.static('public'))
app.use(bodyParser.json())
app.use(session({ secret: 'keyboard cat' }))
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
// Sequelize & Sequelize models
var sequelize = new Sequelize('auth_demo', 'auth_demo', 'auth_demo', {
host: 'localhost',
dialect: 'mysql'
})
var User = sequelize.define('user', {
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
username: {
type: Sequelize.STRING
},
password: {
type: Sequelize.STRING
}
})
// Server initialization stuff
require('./config/passport.js')(passport, User)
sequelize
.sync()
.then(() => {
console.log('Connection successfully established')
})
.catch(err => {
console.log('Unabled to connect:', err)
})
app.listen(3000, () => {
console.log('App is running on port 3000')
})
// Routes.
app.post('/register', passport.authenticate('local-signup'),
function(req, res) {
res.send(req.user)
})
app.post('/logIn', function(req, res, next) {
passport.authenticate('local-signin', function(err, user, info) {
if (err) {
console.log("found err")
return next(err)
}
if (!user) {
console.log("didnt find user")
return res.redirect('/')
}
req.logIn(user, function(err) {
console.log("executing login")
if (err) { return next(err) }
console.log(req)
return res.send(user)
})
})(req, res, next)
})
app.get('/loggedInUser', function(req, res) {
console.log(req.user)
res.send(req.user)
})
app.get('/test', (req, res) => {
console.log(req)
res.send('Hello')
})
此外,这是用于显示策略的passport.js文件:
const LocalStrategy = require('passport-local').Strategy
const bCrypt = require('bcrypt')
module.exports = function(passport, User) {
// Passport stuff
passport.serializeUser(function(user, done) {
done(null, user.id)
})
passport.deserializeUser(function(user, done) {
User.findById(user.id, function(err,user) {
done(err, user)
})
})
// Configure passport strategy
passport.use('local-signup', new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
},
function (username, password, done) {
var generateHash = function(password) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(8), null)
}
User.findOne({ where: { username: username } }).then(function(user) {
if (user) {
return done(null, false, { message: 'That user already exists' })
} else {
console.log("Creating user")
var userPassword = generateHash(password)
User.create({
username: username,
password: userPassword
})
.then(function(newUser) {
if (!newUser) {
return done(null, false)
}
if (newUser) {
return done(null, newUser)
}
})
}
})
}
))
passport.use('local-signin', new LocalStrategy({
usernameField: 'username',
passwordField: 'password',
passReqToCallback: true
},
function (req, username, password, done) {
var isValidPassword = function(userpass, password) {
return bCrypt.compareSync(password, userpass)
}
User.findOne({
where: {
username: username
}
}).then(function(user) {
if (!user) {
console.log("didnt find user")
return done(null, false, {
message: 'User does not exist'
})
}
if (!isValidPassword(user.password, password)) {
console.log("bad password")
return done(null, false, {
message: 'Incorrect password'
})
}
return done(null, user)
})
.catch(function(err) {
console.log("Error:", err)
return done(null, false, {
message: 'Something went wrong with your signin'
})
})
}
))
}
如果我在尝试击中/ logIn端点或/ register端点之前击中了/ test端点,则它可以正常工作并按预期打印“ hello”。但是,如果我在启动服务器后立即点击/ logIn或/ register端点,然后尝试点击/ test,则该请求将挂起并且根本没有任何地方。
这是快递调试器挂起时在终端上打印的内容:
express:router dispatching GET /test +4s
express:router query : /test +1ms
express:router expressInit : /test +0ms
express:router serveStatic : /test +0ms
express:router jsonParser : /test +2ms
express:router session : /test +0ms
express:router initialize : /test +0ms
express:router authenticate : /test +0ms
它在express:router身份验证处停止,所以我倾向于相信它是护照。
感谢您的帮助,谢谢。
答案 0 :(得分:0)
我发现了这一点,以防万一其他人遇到此问题。 User.findById
是基于承诺的。我没有随后被调用的.then
。