javascript - 如何在nodejs mongoose中将此回调身份验证转换为Promise
问题描述
我在这里使用 Nodejs Mongoose 但是当我尝试替换回调以承诺时它给了我一个错误(无法将用户序列化到会话中)请帮助我......
var localStrategy = require('passport-local').Strategy;
const user = require('./mongoBase/userSc.js');
const bcrypt = require('bcryptjs');
module.exports = function(passport) {
passport.use(new localStrategy({ usernameField: 'email' }, (email, password, done) => {
email = email.toLowerCase();
user.findOne({ email: email }, (err, data) => {
if (err) throw err;
if (!data) {
return done(null, false, { message: "User Doesn't Exists.." });
}
bcrypt.compare(password, data.password, (err, match) => {
if (err) {
return done(null, false);
}
if (!match) {
return done(null, false, { message: "Password Doesn't Match" });
}
if (match) {
return done(null, data);
}
});
});
}));
passport.serializeUser(function(user, cb) {
cb(null, user.id);
});
passport.deserializeUser(function(id, cb) {
user.findById(id, function(err, user) {
cb(err, user);
});
});
}
// ---------------
// end of autentication statregy
解决方案
这是我的 spotify 护照代码,也许它可以帮助你一些。如果此答案回答了您的问题,请单击答案旁边的复选框。
module.exports = function(passport) {
passport.use(
new SpotifyStrategy(
{
clientID: 'CLIENT ID',
clientSecret: 'CLIENT SECRET',
callbackURL: 'http://localhost:8888/auth/spotify/callback',
},
async (accessToken, refreshToken, expires_in, profile, done) => {
// Do async operations
async function checking() {
// Do async operations
}
await checking();
return done(null, profile);
}
)
);
// Serialize
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(user, done) {
done(null, user);
});
推荐阅读
- solr - Solr Result Ranking 对特定查询的修改
- c - 通过头文件中的 ifdefs 将结构从 const 非直接类型转换为非 const
- node.js - 在 Node.js 中加载 TensorFlow 冻结图 (.pb)
- arrays - 如何检查值是否在存储在数组中的两个值之间
- ios - UITableView 自定义单元格在滚动时复制 UIButton
- winston - TypeError:winston.createLogger 不是构造函数
- php - 获取多个 Post_meta 并显示最小值
- javascript - 将3个对象变成多维数组
- css - Nth-child 针对所有其他孩子,但始终包括最后一个
- c# - 如何在 C# 中转义 "\"String\""