import { Injectable } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy, VerifyCallback, Profile, StrategyOptions } from 'passport-google-oauth20'; import { ConfigService } from '@nestjs/config'; import { AuthService } from './auth.service'; @Injectable() export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { constructor( private readonly configService: ConfigService, private readonly authService: AuthService, ) { const options: StrategyOptions = { clientID: configService.get('GOOGLE_CLIENT_ID') || '', clientSecret: configService.get('GOOGLE_CLIENT_SECRET') || '', callbackURL: configService.get('GOOGLE_CALLBACK_URL') || '', scope: ['email', 'profile'], }; super(options); } async validate( _accessToken: string, _refreshToken: string, profile: Profile, done: VerifyCallback, ): Promise { try { const user = await this.authService.validateOAuthUser({ id: profile.id, emails: profile.emails as Array<{ value: string; verified: boolean }>, displayName: profile.displayName, photos: profile.photos as Array<{ value: string }>, }); done(null, user); } catch (error) { done(error as Error, undefined); } } }