42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
|
|
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';
|
||
|
|
|
||
|
|
@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);
|
||
|
|
return this.authService.login(user);
|
||
|
|
}
|
||
|
|
|
||
|
|
@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);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|