简体   繁体   English

使用password.js进行Twitter身份验证

[英]Twitter authentication with passport.js

This is a real niche question regarding Twitter OAuth with passport.js () 这是一个关于带有Operation.js的Twitter OAuth的小众问题()

I have a controller which updates the user's avatar using their Twitter "avatar": 我有一个控制器,可使用其Twitter“头像”更新用户的头像:

const signInViaTwitter = (twitterProfile) => {
  return new Promise((resolve, reject) => {

    console.log(twitterProfile);

    // find if user exist on in
    User.findOne({ username: twitterProfile.username }, (error, user) => {
      if (error) { console.log(error); reject(error); }
      else {

        // user existed on db
        if (user) {
          // update the user with latest git profile info
          user.name = twitterProfile.displayName;
          user.username = twitterProfile.username;
          user.avatarUrl = twitterProfile.photos.value;
          user.email = '';

          // save the info and resolve the user doc
          user.save((error) => {
            if (error) { console.log(error); reject(error); }
            else { resolve(user); }
          });
        }

        // user doesn't exists on db
        else {
          // check if it is the first user (Adam/Eve) :-p
          // assign him/her as the admin
          User.count({}, (err, count) => {
            console.log('usercount: ' + count);

            let assignAdmin = false;
            if (count === 0) assignAdmin = true;

            // create a new user
            const newUser = new User({
              name: twitterProfile.displayName,
              username: twitterProfile.username,
              avatarUrl: twitterProfile.photos.value,
              email: '',
              role: assignAdmin ? 'admin' : 'user',
            });

            // save the user and resolve the user doc
            newUser.save((error) => {
              if (error) { console.log(error); reject(error); }
              else { resolve(newUser); }
            });

          });
        }
      }
    });
  });
};

The authentication of the user works - but for some reason, the avatar won't show...here is the following console output: 用户的身份验证有效-但由于某些原因,该头像不会显示...以下控制台输出:

Refused to load the image ' https://api.twitter.com/favicon.ico ' because it violates the following Content Security Policy directive: "img-src https://abs.twimg.com https://*.twimg.com https://pbs.twimg.com data:". 拒绝加载图像' https://api.twitter.com/favicon.ico ',因为它违反了以下内容安全策略指令:“ img-src https://abs.twimg.com https://*.twimg .com https://pbs.twimg.com数据:”。

Does anyone know what this means? 有人知道这意味着什么吗? I'm thinking it's probably due to being in development mode - that is, http://localhost:8080/ ... and it won't accept https?? 我在想这可能是由于处于开发模式-即http:// localhost:8080 / ...而它不会接受https? Or won't pass it back? 还是不会把它传回来?

UPDATE : ^I think the above error is unrelated to the image not being display... 更新 :^我认为以上错误与图像未显示无关...

A little look at the html source gives: 稍微看一下html源代码可以得到:

<img class="styles__userAvatar___2x2U9" src="{unknown}" alt="Wind Up Lord Vexxos Avatar">

So it's obviously passing in an unknown variable for the src - rather than the user's display avatar... 因此,显然是为src传递了一个未知变量-而不是用户的显示头像...

So, for me it looks like the offending line is: 因此,对我而言,令人讨厌的行是:

user.avatarUrl = twitterProfile.photos.value;

What should I be setting this to? 我应该将此设置为什么?

Just a thought, isn't twitterProfile.photos an array? 只是一个想法,twitterProfile.photos不是数组吗? probably you should try accessing twitterProfile.photos[0].value 可能您应该尝试访问twitterProfile.photos [0] .value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM