diff --git a/src/app.module.ts b/src/app.module.ts index 356bfd4..19432fc 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -22,6 +22,9 @@ import { EnvConfig } from './config.type'; REFRESH_SECRET: Joi.string().required(), ACCESS_TOKEN_EXPIRATION: Joi.string().required(), REFRESH_TOKEN_EXPIRATION: Joi.string().required(), + GOOGLE_CLIENT_ID: Joi.string().optional(), + GOOGLE_CLIENT_SECRET: Joi.string().optional(), + GOOGLE_CALLBACK_URL: Joi.string().optional(), }), }), MongooseModule.forRootAsync({ diff --git a/src/modules/auth/strategies/google.strategy.ts b/src/modules/auth/strategies/google.strategy.ts new file mode 100644 index 0000000..ba98609 --- /dev/null +++ b/src/modules/auth/strategies/google.strategy.ts @@ -0,0 +1,37 @@ +import { Injectable } from '@nestjs/common'; +import { PassportStrategy } from '@nestjs/passport'; +import { Strategy, VerifyCallback } from 'passport-google-oauth20'; +import { ConfigService } from '@nestjs/config'; +import { EnvConfig } from '../../../config.type'; + +@Injectable() +export class GoogleStrategy extends PassportStrategy(Strategy, 'google') { + constructor(private configService: ConfigService) { + super({ + clientID: configService.get('GOOGLE_CLIENT_ID'), + clientSecret: configService.get('GOOGLE_CLIENT_SECRET'), + callbackURL: configService.get('GOOGLE_CALLBACK_URL'), + scope: ['email', 'profile'], + }); + } + + async validate( + profile: { + id: string; + name: { givenName: string; familyName: string }; + emails: { value: string }[]; + photos: { value: string }[]; + }, + done: VerifyCallback, + ): Promise { + const { name, emails, photos } = profile; + const user = { + fullName: `${name.givenName} ${name.familyName}`, + email: emails[0].value, + avatar: photos[0].value, + oauthProvider: 'google', + oauthProviderId: profile.id, + }; + done(null, user); + } +}