使用Passport-local策略出现ReferenceError: done is not defined错误求助
关于Passport-Local策略中
ReferenceError: done is not defined的问题 问题详情
错误信息:
ReferenceError: done is not defined
项目主文件代码:
const express = require('express'); const cookieParser = require('cookie-parser'); const port = 8000; const app = express(); const expressLayout = require('express-ejs-layouts'); const db = require('./config/mongoose') // used for session cookie const session = require('express-session') const passport = require('passport') const passportLocal = require('./config/passport-local-strategy') app.use(express.urlencoded()) app.use(cookieParser()); // where to look static files like css,js app.use(express.static('./assets')) // this line must be above the routes line (line no. 11 in this case) because in the routes all the views are going to be render and before that we have to tell to the browser the layout app.use(expressLayout) // extract style and scripts from sub pages into the layout app.set('layout extractStyles', true); app.set('layout extractScripts', true); // set up the view engine app.set('view engine', 'ejs'); app.set('views', './views'); app.use(session({ name: 'iFacebook', // TODO change the secret before deployment in production mode secret: 'Coder', saveUninitialized: false, resave: false, cookie: { maxAge : (1000*60*100) } })) app.use(passport.initialize()); app.use(passport.session()) // use express router // require('./routes/index) is similar to require('./routes) in this case, it by default fetch routes app.use('/', require('./routes/index')) app.listen(port, (err) => { if (err) { console.log(`Error in running the server : ${err}`); } console.log(`Server is listening at ${port}`); })
我正在使用Passport及Passport-Local策略,出现上述错误且无法定位来源。这是我首次使用该技术,查阅官方文档也未找到相关解决方案,特此求助。
解决方案
这个错误的核心问题出在你的./config/passport-local-strategy.js文件中——Passport的本地策略验证逻辑必须通过done回调函数来完成验证流程,你大概率是没正确声明或调用这个回调。
修正步骤:
- 打开
./config/passport-local-strategy.js文件,检查验证函数的写法 - 确保验证函数正确接收
done作为参数,并且在所有分支(成功、失败、出错)都调用它 - 参考以下标准写法调整你的代码:
const passport = require('passport'); const LocalStrategy = require('passport-local').Strategy; // 替换成你实际的用户模型路径 const User = require('../models/user'); // 配置本地策略 passport.use(new LocalStrategy({ // 根据你的登录表单字段调整,比如用username或email usernameField: 'email' }, // 必须接收done作为第三个参数 function(email, password, done) { // 查询数据库中的用户 User.findOne({ email: email }, function(err, user) { // 数据库查询出错时,传递错误给done if (err) { console.log('查询用户出错 --> Passport'); return done(err); } // 用户不存在或密码不匹配时,返回验证失败 if (!user || user.password !== password) { console.log('用户名/密码无效'); return done(null, false); } // 验证成功,返回用户对象 return done(null, user); }); } )); // 序列化用户(将用户ID存入session) passport.serializeUser(function(user, done) { done(null, user.id); }); // 反序列化用户(从session中取出ID,查询完整用户信息) passport.deserializeUser(function(id, done) { User.findById(id, function(err, user) { if (err) { console.log('查询用户出错 --> Passport'); return done(err); } return done(null, user); }); }); module.exports = passport;
关键注意点:
- 验证函数的参数顺序必须是
用户名字段, 密码, done,如果用usernameField指定了其他字段,第一个参数要对应这个字段 - 无论验证成功、失败还是数据库出错,都必须调用
done回调,否则Passport无法完成验证流程 - 不要用明文比较密码,建议使用bcrypt等加密库处理密码存储与验证
内容的提问来源于stack exchange,提问作者Harsh Sisodia
相关产品推荐
相关产品推荐

