nestjs - 如何从passport-google-oauth20包中获取id_token - NestJs

标签 nestjs passport-google-oauth

我是 NestJs 新手,正在尝试使用 passport-google-oauth20 包实现 Google Sign in。我已关注that blog实现谷歌登录。通过这个包,我能够成功登录并能够获取 access_token 但我需要 id_token 而不是 access_token 。我深入研究了 passport-google-oauth20 Strategy 类,在那里我可以看到不同的重载构造函数,其中一个重载构造函数包含 GoogleCallbackParameters 类型的 params 参数其中包含可选的 id_token 字段。但不知道如何调用该构造函数。尝试了不同的方法但没有成功。 :(

下面是我的代码,

import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { Request } from "express";
import { Profile } from "passport";
import {
  GoogleCallbackParameters,
  Strategy,
  VerifyCallback,
} from "passport-google-oauth20";
import { googleStrategy } from "src/utils/constants";

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, "google") {
  constructor() {
    super({
      clientID:
        process.env.BACKEND_ENV === "dev"
          ? googleStrategy.GOOGLE_CLIENT_ID
          : process.env.GOOGLE_CLIENT_ID,
      clientSecret:
        process.env.BACKEND_ENV === "dev"
          ? googleStrategy.GOOGLE_CLIENT_SECRET
          : process.env.GOOGLE_CLIENT_SECRET,
      callbackURL:
        process.env.BACKEND_ENV === "dev"
          ? googleStrategy.GOOGLE_CALLBACK_URL
          : process.env.GOOGLE_CALLBACK_URL,
      scope: ["email", "profile", "openid"],
      passReqToCallback: true,
    });
  }

  async validate(
    req: Request,
    accessToken: string,
    refreshToken: string,
    params: GoogleCallbackParameters,
    profile: Profile,
    done: VerifyCallback,
  ): Promise<any> {
    const { name, emails, photos } = profile;
    const user = {
      email: emails[0].value,
      firstName: name.givenName,
      lastName: name.familyName,
      picture: photos[0].value,
      accessToken,
      refreshToken,
    };

    done(null, user);
  }
}

正如您所看到的,为了获取 Request,我提到了 passReqToCallback: true 选项,并且在验证方法中我获取了 Request 对象但不知道如何使 GoogleCallbackParameters 类型的 params 填充所需的对象。

谢谢。

最佳答案

我通过直接在 super 方法中将回调作为第二个参数传递来解决了这个问题,我不知道为什么在 validate 方法中它不起作用,也许是使用 Nestjs 的 PassportStrategy 的问题。

像下面这样的东西:

@Injectable()
    export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
      constructor() {
        super(
          {
            clientID:
              process.env.BACKEND_ENV === 'dev'
                ? googleStrategy.GOOGLE_CLIENT_ID
                : process.env.GOOGLE_CLIENT_ID,
            clientSecret:
              process.env.BACKEND_ENV === 'dev'
                ? googleStrategy.GOOGLE_CLIENT_SECRET
                : process.env.GOOGLE_CLIENT_SECRET,
            callbackURL:
              process.env.BACKEND_ENV === 'dev'
                ? googleStrategy.GOOGLE_CALLBACK_URL
                : process.env.GOOGLE_CALLBACK_URL,
            scope: ['email', 'profile', 'openid'],
            passReqToCallback: true,
          },
          async (
            req: Request,
            accessToken: string,
            refreshToken: string,
            params: GoogleCallbackParameters,
            profile: Profile,
            done: VerifyCallback,
          ): Promise<any> => {
            const { name, emails, photos } = profile;
            const user = {
              email: emails[0].value,
              firstName: name.givenName,
              lastName: name.familyName,
              picture: photos[0].value,
              accessToken,
              refreshToken,
            };
    
            done(null, user);
          },
        );
      }
    }

关于nestjs - 如何从passport-google-oauth20包中获取id_token - NestJs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71536002/

相关文章:

node.js - 类型错误 : next is not a function when io. 使用(passport.authenticate ('google' ))

google-api - 使用 google people API 访问 google 连接

mysql - NestJs TypeORM 无法连接到 mysql

javascript - NestJS:使用 JWT 向 AuthGuard 添加验证选项

node.js - NodeJS Passport 如何将项目从 Google+ 登录迁移到 Google 登录?

javascript - 使用 google-passport-oauth 登录后 req.user 未定义

javascript - 是否可以设置 Nest.js HTTP 模块来全局使用 Promises 而不是 Observables?

具有 TypeORM : When using custom repository, 的 NestJS 是否需要服务?

node.js - 防止在监视模式下运行时对某些文件夹进行更改后自动重新启动服务器