稍后我如何使用 Passport JS 提供的访问令牌?

How could I use the access token provided from Passport JS later on?

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

我已经尝试在 "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;
      });
    }
  )
);

我会尝试其中一种选择。

  1. 在用户 table / 模型中有一列名为 "spotifyAccessToken" 并将其存储在那里。

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

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

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);
        });
      });
    }
  )
);