139 lines
3.1 KiB
TypeScript
139 lines
3.1 KiB
TypeScript
import { AuthDocumentation } from './auth.documentation';
|
|
import { Controller, Post, Body, UseGuards, Request, Get, Req, Res } from '@nestjs/common';
|
|
import { AuthService } from './auth.service';
|
|
import { LoginDto } from './dto/login.dto';
|
|
import { AuthGuard } from '@nestjs/passport';
|
|
import { RegisterDto } from './dto/register.dto';
|
|
import { ApiBearerAuth } from '@nestjs/swagger';
|
|
import { SetPasswordDto } from './dto/createPassword.dto';
|
|
import { Response } from 'express';
|
|
import { WhiteDto } from './dto/whiteList.dto';
|
|
|
|
interface Update{
|
|
id:string;
|
|
nombreNuevo:string;
|
|
|
|
}
|
|
|
|
@Controller()
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) { }
|
|
//Comentar en producciòn
|
|
|
|
@Post('update')
|
|
async update(@Body() user:Update) {
|
|
return this.authService.updateCorreo(user.id,user.nombreNuevo);
|
|
}
|
|
|
|
@Get('findAll')
|
|
async findAll(){
|
|
this.authService.all()
|
|
|
|
}
|
|
|
|
@Post('login')
|
|
@AuthDocumentation.login()
|
|
async login(@Body() dto: LoginDto) {
|
|
const user = await this.authService.validateUser(dto.email, dto.password);
|
|
if (!user) {
|
|
throw new Error('Invalid credentials');
|
|
}
|
|
return this.authService.login(user);
|
|
}
|
|
|
|
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@ApiBearerAuth('bearer')
|
|
@UseGuards(AuthGuard('jwt'))
|
|
@Get('profile')
|
|
@AuthDocumentation.bearerAuth()
|
|
getProfile(@Request() req) {
|
|
// req.user viene de JwtStrategy.validate()
|
|
return {
|
|
userId: req.user.userId,
|
|
email: req.user.email,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Post('register')
|
|
@AuthDocumentation.register() // ver siguiente sección
|
|
async register(@Body() dto: RegisterDto) {
|
|
return this.authService.register(dto);
|
|
}
|
|
|
|
@Post('white-list')
|
|
@AuthDocumentation.white()
|
|
async newRegister(@Body() dto:WhiteDto){
|
|
|
|
return this.authService.white(dto)
|
|
|
|
}
|
|
|
|
|
|
//Inicio de sesión con GOOGLE
|
|
|
|
@Get('google')
|
|
@UseGuards(AuthGuard('google'))
|
|
async googleLogin() {
|
|
return { msg: 'Redirigiendo a Google' };
|
|
}
|
|
|
|
// Callback de Google
|
|
// @Get('google/callback')
|
|
// @UseGuards(AuthGuard('google'))
|
|
// async googleCallback(@Req() req) {
|
|
// const user = req.user;
|
|
|
|
// if (user.needsPassword) {
|
|
// return {
|
|
// status: 'NEEDS_PASSWORD',
|
|
// email: user.correo,
|
|
// message: 'El usuario debe crear una contraseña antes de continuar',
|
|
// };
|
|
// }
|
|
|
|
// return this.authService.login(user); // genera JWT
|
|
// }
|
|
|
|
// auth.controller.ts
|
|
@Get('google/callback')
|
|
@UseGuards(AuthGuard('google'))
|
|
async googleCallback(@Req() req, @Res() res: Response) {
|
|
const user = req.user;
|
|
|
|
if (user.needsPassword) {
|
|
|
|
return res.redirect(
|
|
`${process.env.FRONTEND_URL}/password?email=${user.correo}`,
|
|
);
|
|
}
|
|
|
|
const jwt = await this.authService.login(user);
|
|
|
|
|
|
return res.redirect(
|
|
`${process.env.FRONTEND_URL}/oauth-callback?token=${jwt.access_token}`,
|
|
);
|
|
}
|
|
|
|
|
|
//Inicio de sesión con GOOGLE
|
|
|
|
//Set-contraseña
|
|
// auth.controller.ts
|
|
|
|
|
|
@Post('set-password')
|
|
async setPassword(@Body() dto: SetPasswordDto) {
|
|
return this.authService.setPassword(dto);
|
|
}
|
|
|
|
|
|
|
|
}
|