35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
import { Strategy, type Profile } from 'passport-google-oauth20';
|
|
|
|
export type GoogleIdentity = {
|
|
googleId: string;
|
|
email: string;
|
|
name: string;
|
|
};
|
|
|
|
@Injectable()
|
|
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
|
constructor(config: ConfigService) {
|
|
super({
|
|
clientID: config.get<string>('GOOGLE_CLIENT_ID') || 'not-configured',
|
|
clientSecret: config.get<string>('GOOGLE_CLIENT_SECRET') || 'not-configured',
|
|
callbackURL: config.get<string>('GOOGLE_CALLBACK_URL') || 'http://localhost:3000/api/auth/google/callback',
|
|
scope: ['email', 'profile'],
|
|
});
|
|
}
|
|
|
|
validate(_accessToken: string, _refreshToken: string, profile: Profile): GoogleIdentity {
|
|
const email = profile.emails?.[0]?.value?.toLowerCase();
|
|
if (!email || !profile.id) {
|
|
throw new UnauthorizedException('Conta Google sem e-mail.');
|
|
}
|
|
return {
|
|
googleId: profile.id,
|
|
email,
|
|
name: profile.displayName?.trim() || email,
|
|
};
|
|
}
|
|
}
|