Skip to content

Commit

Permalink
feat: add google strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
LhonRafaat committed Oct 11, 2024
1 parent 023230a commit 235fb03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
37 changes: 37 additions & 0 deletions src/modules/auth/strategies/google.strategy.ts
Original file line number Diff line number Diff line change
@@ -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<EnvConfig>) {
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<void> {
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);
}
}

0 comments on commit 235fb03

Please sign in to comment.