0.0.4 • Published 1 year ago

nest-google-oauth2 v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Description

Google-OAuth2 Module for Nest.

Installation

$ npm install nest-google-oauth2

Usage

Injecting the GoogleOAuth2Module

import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { AuthenticationService } from './authentication.service';
import { AuthenticationController } from './authentication.controller';
import { GoogleOAuth2Module } from 'nest-google-oauth2';

@Module({
  imports: [
    ConfigModule,
    GoogleOAuth2Module.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          clientId: configService.get('GOOGLE_CLIENT_ID'),
          clientSecret: configService.get('GOOGLE_SECRET'),
        };
      },
    }),
  ],
  controllers: [AuthenticationController],
  providers: [AuthenticationService],
})
export class AuthenticationModule {}

Using the GoogleOAuth2Service

import { Injectable } from '@nestjs/common';
import { GoogleOAuth2Service } from 'nest-google-oauth2';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AuthenticationService {
  constructor(
    private readonly configService: ConfigService,
    private readonly googleOAuth2Service: GoogleOAuth2Service,
  ) {}
  public async googleOauth(data: { accessToken: string }) {
    const tokenInfo = await this.googleOAuth2Service.getTokenInfo(
      data.accessToken,
    );
    console.log(tokenInfo);
    const ticket = await this.googleOAuth2Service.verifyIdToken({
      idToken: data.accessToken,
      audience: this.configService.get('GOOGLE_CLIENT_ID'),
    });
    return ticket.getPayload();
  }
}

Stay in touch

License

nest-google-oauth2 is MIT licensed.