33 lines
977 B
TypeScript
33 lines
977 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { Strategy, VerifyCallback } from 'passport-google-oauth20';
|
|
|
|
@Injectable()
|
|
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
|
constructor(configService: ConfigService) {
|
|
super({
|
|
clientID: configService.get<string>('GOOGLE_CLIENT_ID') || 'dummy',
|
|
clientSecret: configService.get<string>('GOOGLE_CLIENT_SECRET') || 'dummy',
|
|
callbackURL: configService.get<string>('GOOGLE_CALLBACK_URL'),
|
|
scope: ['email', 'profile'],
|
|
});
|
|
}
|
|
|
|
async validate(
|
|
accessToken: string,
|
|
refreshToken: string,
|
|
profile: any,
|
|
done: VerifyCallback,
|
|
): Promise<any> {
|
|
const { id, displayName, emails } = profile;
|
|
const user = {
|
|
providerId: id,
|
|
provider: 'google',
|
|
email: emails?.[0]?.value,
|
|
name: displayName,
|
|
};
|
|
done(null, user);
|
|
}
|
|
}
|