change guard
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
import {
|
||||
Injectable,
|
||||
ExecutionContext,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { Injectable, ExecutionContext } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class GoogleAuthGuard extends AuthGuard('google') {
|
||||
handleRequest(err, user, info, context: ExecutionContext) {
|
||||
if (err || !user) {
|
||||
throw err || new UnauthorizedException(); // deja que el controller maneje el redirect
|
||||
const req = context.switchToHttp().getRequest();
|
||||
const res = context.switchToHttp().getResponse();
|
||||
|
||||
// Si hay info (por ejemplo, Google retorna ?error=access_denied)
|
||||
if (info) {
|
||||
return res.redirect(
|
||||
`${process.env.FRONTEND_URL}/oauth-callback?error=${info.message || info}`,
|
||||
);
|
||||
}
|
||||
|
||||
// Si no hay usuario ni error, permitimos que el controller maneje
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,20 +20,9 @@ export class PersonaController {
|
||||
@Get('google/callback')
|
||||
@UseGuards(GoogleAuthGuard)
|
||||
async googleCallback(@Req() req, @Res() res: Response) {
|
||||
if (req.query.error) {
|
||||
return res.redirect(
|
||||
`${process.env.FRONTEND_URL}/oauth-callback?error=${req.query.error}`,
|
||||
);
|
||||
}
|
||||
|
||||
const persona = req.user;
|
||||
|
||||
if (!persona) {
|
||||
const error = req.query.error || 'oauth_denied';
|
||||
return res.redirect(
|
||||
`${process.env.FRONTEND_URL}/oauth-callback?error=${error}`,
|
||||
);
|
||||
}
|
||||
if (!persona) return;
|
||||
|
||||
const jwt = await this.personaService.generateJwt(persona);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user