import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { OAuth2Client } from 'google-auth-library'; import { SocialInterface } from '../social/interfaces/social.interface'; import { AuthGoogleLoginDto } from './dto/auth-google-login.dto'; import { AllConfigType } from 'src/config/config.type'; @Injectable() export class AuthGoogleService { private google: OAuth2Client; constructor(private configService: ConfigService) { this.google = new OAuth2Client( configService.get('google.clientId', { infer: true }), configService.get('google.clientSecret', { infer: true }), ); } async getProfileByToken( loginDto: AuthGoogleLoginDto, ): Promise { const ticket = await this.google.verifyIdToken({ idToken: loginDto.idToken, audience: [ this.configService.getOrThrow('google.clientId', { infer: true }), ], }); const data = ticket.getPayload(); if (!data) { throw new HttpException( { status: HttpStatus.UNPROCESSABLE_ENTITY, errors: { user: 'wrongToken', }, }, HttpStatus.UNPROCESSABLE_ENTITY, ); } return { id: data.sub, email: data.email, firstName: data.given_name, lastName: data.family_name, }; } }