Passport facebook策略throw数据必须是使用Node.js的字符串

时间:2017-02-24 18:38:46

标签: javascript node.js authentication passport.js passport-facebook

我正在使用Node.jspassport 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"

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

当创建密码加密参数的函数为null时,此错误发生。例如:

const crypto = require(' crypto')

让shasum = crypto.createHash(' sha256') shasum.update(密码)//密码为空

此身份验证方法不需要密码,您必须编写条件以防止加密。

相关问题