【问题标题】:Passport facebook strategy throw Data must be string using Node.jsPassport facebook 策略 throw Data 必须是使用 Node.js 的字符串
【发布时间】:2017-07-15 17:16:38
【问题描述】:

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

我该如何解决这个错误?

【问题讨论】:

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


    【解决方案1】:

    当创建密码的函数加密参数为空时会发生此错误。举例:

    const crypto = require('crypto')

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

    这种身份验证方法不需要密码,您必须编写一个条件来防止加密。

    【讨论】:

      猜你喜欢
      • 2019-10-15
      • 1970-01-01
      • 2017-05-08
      • 2015-07-27
      • 1970-01-01
      • 2013-10-17
      • 2018-03-05
      • 2016-05-20
      • 2020-12-10
      相关资源
      最近更新 更多