2025-06-15 20:53:51 -06:00
|
|
|
import { AuthDocumentation } from './auth.documentation';
|
|
|
|
|
import { Controller, Post, Body, UseGuards, Request, Get } 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';
|
2025-06-16 10:24:03 -06:00
|
|
|
import { ApiBearerAuth } from '@nestjs/swagger';
|
2025-06-15 20:53:51 -06:00
|
|
|
|
|
|
|
|
@Controller()
|
|
|
|
|
export class AuthController {
|
2025-06-18 07:33:21 -06:00
|
|
|
constructor(private readonly authService: AuthService) { }
|
2025-06-15 20:53:51 -06:00
|
|
|
|
|
|
|
|
@Post('login')
|
|
|
|
|
@AuthDocumentation.login()
|
|
|
|
|
async login(@Body() dto: LoginDto) {
|
|
|
|
|
const user = await this.authService.validateUser(dto.email, dto.password);
|
2025-06-18 07:33:21 -06:00
|
|
|
if (!user) {
|
|
|
|
|
throw new Error('Invalid credentials');
|
|
|
|
|
}
|
2025-06-15 20:53:51 -06:00
|
|
|
return this.authService.login(user);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-16 10:24:03 -06:00
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
@UseGuards(AuthGuard('jwt'))
|
2025-06-16 10:24:03 -06:00
|
|
|
@ApiBearerAuth('bearer')
|
2025-06-18 07:33:21 -06:00
|
|
|
@UseGuards(AuthGuard('jwt'))
|
2025-06-15 20:53:51 -06:00
|
|
|
@Get('profile')
|
|
|
|
|
@AuthDocumentation.bearerAuth()
|
|
|
|
|
getProfile(@Request() req) {
|
|
|
|
|
// req.user viene de JwtStrategy.validate()
|
|
|
|
|
return {
|
|
|
|
|
userId: req.user.userId,
|
|
|
|
|
email: req.user.email,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
@Post('register')
|
2025-06-15 20:53:51 -06:00
|
|
|
@AuthDocumentation.register() // ver siguiente sección
|
|
|
|
|
async register(@Body() dto: RegisterDto) {
|
|
|
|
|
return this.authService.register(dto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|