se agregaron cambios de seguridad
This commit is contained in:
@@ -8,6 +8,7 @@ import { ApiBearerAuth } from '@nestjs/swagger';
|
||||
import { SetPasswordDto } from './dto/createPassword.dto';
|
||||
import { Response } from 'express';
|
||||
import { WhiteDto } from './dto/whiteList.dto';
|
||||
import { GoogleAuthGuard } from './google-auth.guard';
|
||||
|
||||
interface Update{
|
||||
id:string;
|
||||
@@ -102,7 +103,7 @@ export class AuthController {
|
||||
|
||||
// auth.controller.ts
|
||||
@Get('google/callback')
|
||||
@UseGuards(AuthGuard('google'))
|
||||
@UseGuards(GoogleAuthGuard)
|
||||
async googleCallback(@Req() req, @Res() res: Response) {
|
||||
const user = req.user;
|
||||
|
||||
@@ -119,6 +120,8 @@ export class AuthController {
|
||||
return res.redirect(
|
||||
`${process.env.FRONTEND_URL}/oauth-callback?token=${jwt.access_token}`,
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// google-auth.guard.ts
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAuthGuard extends AuthGuard('google') {
|
||||
handleRequest(err: any, user: any, info: any, context: any) {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const res = context.switchToHttp().getResponse();
|
||||
|
||||
if (err || !user) {
|
||||
// 👇 siempre redirigir al front en caso de error
|
||||
res.redirect(`${process.env.FRONTEND_URL}?error=oauth_failed`);
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
||||
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`,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,15 +26,19 @@ export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
||||
profile: any,
|
||||
done: VerifyCallback,
|
||||
): Promise<any> {
|
||||
try{
|
||||
const { emails } = profile;
|
||||
const email = emails[0].value;
|
||||
|
||||
const user = await this.authService.validateGoogleUser(email);
|
||||
|
||||
if (!user) {
|
||||
throw new UnauthorizedException('Usuario no registrado en el sistema');
|
||||
done(null, false);
|
||||
}
|
||||
|
||||
done(null, user);
|
||||
}catch(err){
|
||||
done(null, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -13,7 +13,11 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.enableCors({ exposedHeaders: ['Content-Disposition'] });
|
||||
app.enableCors({
|
||||
origin: ['https://venus.acatlan.unam.mx/servicios_pcpuma'], // solo frontend permitido
|
||||
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
|
||||
credentials: true,
|
||||
});
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
|
||||
Reference in New Issue
Block a user