护照获取刷新令牌 OAuth

Passport Get Refresh Token OAuth

如何使用 Passport 在 OAuth 策略中获取刷新令牌?我能够获得访问令牌,但也需要获得刷新令牌。

从Google文档中,发现需要在request中添加access_type=offline。 如何使用passport实现。

//STRATEGY
passport.use(
  new GoogleStrategy(
    {
      clientID: googleKeys.clientID,
      clientSecret: googleKeys.clientSecret,
      callbackURL: urls.cb,
    },
    (accessToken: any, refreshToken: any, profile: any, cb: any) => {

      //refreshToken is undefined
      cb(null, { email: profile._json.email });

    }
  )
);

router.get(
  "/auth/google",
  passport.authenticate("google", {
    scope: [
      "https://www.googleapis.com/auth/userinfo.profile",
      "https://www.googleapis.com/auth/userinfo.email",
    ],
  })
);

//REDIRECT URI
router.get(
  "/auth/google/callback",
  passport.authenticate("google", { failureRedirect: "/", session: false }),
  (req: Request, res: Response) => {
    res.send("Success");
  }
);

使用passport.authenticate时需要将访问类型作为参数传递。

router.get(
  "/auth/google",
  passport.authenticate("google", {
    scope: [
      "https://www.googleapis.com/auth/userinfo.profile",
      "https://www.googleapis.com/auth/userinfo.email",
    ],
    accessType: 'offline',
    prompt: 'consent',
  })
);

参考:https://github.com/jaredhanson/passport-google-oauth2/issues/4#issuecomment-344460414