【发布时间】:2020-07-15 06:21:25
【问题描述】:
我正在使用nodejs和mysql构建一个登录注册系统,我决定使用passport js进行用户认证。我还使用 bcrypt 对密码进行了哈希处理。到目前为止,我的注册表单能够从表单中获取数据输入并将它们存储到数据库表中。
由于我只是 Node 的初学者,我不知道如何更改我的身份验证配置文件的代码,以便可以从数据库中的表中读取数据并从中进行身份验证。以下是护照配置文件:
const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
const user = getUserByEmail(email)
if (user == null) {
return done(null, false, { message: 'No user with that email' })
}
try {
if (await bcrypt.compare(password, user.password)) {
return done(null, user)
} else {
return done(null, false, { message: 'Password incorrect' })
}
} catch (e) {
return done(e)
}
}
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser))
passport.serializeUser((user, done) => done(null, user.id))
passport.deserializeUser((id, done) => {
return done(null, getUserById(id))
})
}
module.exports = initialize
这是我使用注册凭据登录时收到的错误:
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
Database is connected!
New user is registered! INSERT INTO users (firstname, lastname, email, password) VALUES ('w', 'w', 'w@w', '$2b$10$bRcsJON33dZ/RCWsgNdp..G.dc112XaYYTWWiX3a7/YqnXWXE70Aa')
(node:61627) UnhandledPromiseRejectionWarning: ReferenceError: users is not defined
at /Users/vaidiklapalikar/Desktop/current project/server.js:30:20
at Strategy.authenticateUser [as _verify] (/Users/vaidiklapalikar/Desktop/current project/config/passport-config.js:6:18)
at Strategy.authenticate (/Users/vaidiklapalikar/Desktop/current project/node_modules/passport-local/lib/strategy.js:90:12)
at attempt (/Users/vaidiklapalikar/Desktop/current project/node_modules/passport/lib/middleware/authenticate.js:366:16)
at authenticate (/Users/vaidiklapalikar/Desktop/current project/node_modules/passport/lib/middleware/authenticate.js:367:7)
at Layer.handle [as handle_request] (/Users/vaidiklapalikar/Desktop/current project/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/vaidiklapalikar/Desktop/current project/node_modules/express/lib/router/route.js:137:13)
at checkNotAuthenticated (/Users/vaidiklapalikar/Desktop/current project/server.js:97:5)
at Layer.handle [as handle_request] (/Users/vaidiklapalikar/Desktop/current project/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/vaidiklapalikar/Desktop/current project/node_modules/express/lib/router/route.js:137:13)
(node:61627) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:61627) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
据我了解,我没有使用选择查询来实际读取表中的数据并通过 passport.js 进行验证。但是,我不知道该怎么做。
【问题讨论】:
标签: mysql node.js express passport.js