Files
cargaMasiva_api/src/auth/auth.controller.ts
T

49 lines
1.2 KiB
TypeScript
Raw Normal View History

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';
@Controller()
export class AuthController {
constructor(private readonly authService: AuthService) { }
@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);
}
2025-06-16 10:24:03 -06:00
@UseGuards(AuthGuard('jwt'))
2025-06-16 10:24:03 -06:00
@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);
}
}