【发布时间】:2017-07-15 17:16:38
【问题描述】:
我正在使用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"。
我该如何解决这个错误?
【问题讨论】:
标签: javascript node.js authentication passport.js passport-facebook