Files
cargaMasiva_api/src/auth/google.strategy.ts
T

45 lines
1.2 KiB
TypeScript

// google.strategy.ts
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { Strategy, VerifyCallback } 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 authService: AuthService,
private readonly config: ConfigService,
) {
super({
clientID: config.get<string>('GOOGLE_CLIENT_ID'),
clientSecret: config.get<string>('GOOGLE_CLIENT_SECRET'),
callbackURL: config.get<string>('GOOGLE_CALLBACK_URL'),
scope: ['email', 'profile'],
failureRedirect: `${process.env.FRONTEND_URL}?error=oauth_failed`,
});
}
async validate(
accessToken: string,
refreshToken: string,
profile: any,
done: VerifyCallback,
): Promise<any> {
try{
const { emails } = profile;
const email = emails[0].value;
const user = await this.authService.validateGoogleUser(email);
if (!user) {
done(null, false);
}
done(null, user);
}catch(err){
done(null, false);
}
}
}