我正在使用Node.js
和passport facebook strategy
在应用中记录客户端。
我关注了护照文档,但仍然有错误:数据必须是字符串或缓冲区。
策略重定向到Facebook页面很好,但在测试接受应用程序的条件并重定向到主页后,应用程序抛出此错误:
StatusCodeError: 500 - {"error":"Data must be a string or a buffer"}
这是我编写策略的auth.js
代码。我正在使用jsonwebtoken
模块来签署用户ID。
exports.facebookStrategy = new FacebookStrategy({
clientID: config.auth.facebook.clientID,
clientSecret: config.auth.facebook.clientSecret,
callbackURL: config.auth.facebook.callbackURL,
profileFields: ['id', 'displayName', 'email']
}, function (accessToken, refreshToken, profile, done) {
var userProfile = {
username: profile._json.id,
name: profile._json.name,
email: profile._json.email,
facebook: true
}
findOrCreate(userProfile, (err, user) => {
if (err) {
return done(err);
}
// use token lib to sign id
jwt.sign({ userId: user.username }, config.secret, {}, (e, token) => {
if (e) {
return done(e);
}
user.token = token;
return done(null, user);
})
});
function findOrCreate (user, callback) {
client.getUser(user.username, (err, usr) => {
if (err) {
return client.saveUser(user, callback);
}
callback(null, usr);
})
}
});
使用console.log
我发现错误来自这段代码:
...
findOrCreate(userProfile, (err, user) => {
if (err) {
console.log(err.message); // it returns 500 - {"error":"Data must be a string or a buffer"}
return done(err);
}
我尝试将profile._json
更改为profile._raw
。但是,所有值均为undefined
。
我正在使用Node 6.10.0版本。和passport: "^0.3.2"
,"passport-facebook": "^2.1.1"
。
如何解决此错误?
答案 0 :(得分:0)
当创建密码加密参数的函数为null时,此错误发生。例如:
const crypto = require(' crypto')
让shasum = crypto.createHash(' sha256') shasum.update(密码)//密码为空
此身份验证方法不需要密码,您必须编写条件以防止加密。