【问题标题】:How could I use the access token provided from Passport JS later on?以后如何使用 Passport JS 提供的访问令牌?
【发布时间】:2019-06-25 19:35:14
【问题描述】:

我正在试验 Spotify API,我正在使用 Node JS、Express、React、Passport JS。我使用护照 JS 进行身份验证,在设置中,有一个参数是访问令牌。使用此访问令牌,我可以访问用户的库和其他信息。但我不确定之后如何访问此访问令牌。我的意思是,我想在用户单击某个按钮时使用它,而不是在用户登录时使用它。如何“保存”此访问令牌或稍后访问它?

我尝试在“done(err,user)”之后返回访问令牌,但我不知道它被返回到哪里。

这是我的护照设置:

passport.use(
  new SpotifyStrategy(
    {
      clientID: keys.spotify.clientID,
      clientSecret: keys.spotify.clientSecret,
      callbackURL: "/callback"
    },
    function(accessToken, refreshToken, expires_in, profile, done) {
      User.findOrCreate({
        where: {
          user_spotify_id: profile.id,
          username: profile.username,
          email: profile._json.email,
          country: profile.country,
          birthdate: profile._json.birthdate
        }
      }).then(([user, created]) => {

        done(null, user);
        // return accessToken;
      });
    }
  )
);

【问题讨论】:

    标签: reactjs express passport.js


    【解决方案1】:

    我会尝试这些选项之一。

    1. 在用户表/模型中有一个名为“spotifyAccessToken”的列并将其存储在那里。

    2. 或者有一个单独的表来存储访问令牌和用户表的外键。

    当用户注销时,销毁令牌。

    passport.use(
      new SpotifyStrategy(
        {
          clientID: keys.spotify.clientID,
          clientSecret: keys.spotify.clientSecret,
          callbackURL: "/callback"
        },
        function(accessToken, refreshToken, expires_in, profile, done) {
          User.findOrCreate({
            where: {
              user_spotify_id: profile.id,
              username: profile.username,
              email: profile._json.email,
              country: profile.country,
              birthdate: profile._json.birthdate
            }
          }).then(([user, created]) => {
    
            // store access token
            User.update({
              spotifyAccessToken: accessToken
            }, {
              where: {
                id: user.id
              },
              returning: true // returns the user after update
            }).then(result => {
              user = result[1][0]; // get user
    
              done(null, user);
            });
          });
        }
      )
    );
    

    【讨论】:

      猜你喜欢
      • 2019-06-09
      • 1970-01-01
      • 2012-07-06
      • 2021-06-12
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2019-12-21
      • 2016-08-27
      相关资源
      最近更新 更多