69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
import { Controller, Get, UseGuards, Req, Res } from '@nestjs/common';
|
|
import type { Response } from 'express';
|
|
import { PersonaService } from './persona.service';
|
|
import { GoogleAuthGuard } from './google-auth.guard';
|
|
import { JwtAuthGuard } from './jwt.guard';
|
|
|
|
@Controller('persona')
|
|
export class PersonaController {
|
|
constructor(private readonly personaService: PersonaService) {}
|
|
|
|
@Get()
|
|
getAll() {
|
|
return this.personaService.findAll();
|
|
}
|
|
|
|
@Get('google')
|
|
@UseGuards(GoogleAuthGuard)
|
|
async googleLogin() {}
|
|
|
|
@Get('google/callback')
|
|
@UseGuards(GoogleAuthGuard)
|
|
async googleCallback(@Req() req, @Res() res: Response) {
|
|
const persona = req.user;
|
|
|
|
if (!persona) {
|
|
return res.redirect(`${process.env.FRONTEND_URL}?error=oauth_failed`);
|
|
}
|
|
|
|
const jwt = await this.personaService.generateJwt(persona);
|
|
|
|
return res.redirect(
|
|
`${process.env.FRONTEND_URL}/oauth-callback?token=${jwt.access_token}`,
|
|
);
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('me')
|
|
async getMe(@Req() req) {
|
|
const idPersona = req.user.idPersona;
|
|
|
|
const persona = await this.personaService.findById(idPersona);
|
|
|
|
return {
|
|
error: false,
|
|
msj: `Bienvenido ${persona.nombre} ${persona.apellidoP} ${persona.apellidoM}`,
|
|
data: {
|
|
userId: persona.idPersona,
|
|
nombre: persona.nombre,
|
|
apellidoP: persona.apellidoP,
|
|
apellidoM: persona.apellidoM,
|
|
saldo: persona.cantidadCuenta,
|
|
carreraAds: persona.carreraAds,
|
|
numeroCuenta: persona.numeroIdentificar,
|
|
tipoUsuario: persona.tipoUsuario,
|
|
primerLogin: persona.primerLogin,
|
|
cambioPassword: persona.cambioPasswordReq,
|
|
},
|
|
};
|
|
}
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('saldo')
|
|
async saldo(@Req() req) {
|
|
const idPersona = req.user.idPersona;
|
|
return this.personaService.saldo(idPersona);
|
|
}
|
|
}
|
|
//IO
|