import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; import { Request } from 'express'; import { AuthGuard, RequestWithUser } from './auth.guard'; import { AuthService } from './auth.service'; import { CadastroDto, LoginDto, OAuthGoogleDto, RecuperarSenhaDto, RedefinirSenhaDto, } from './dto/auth.dto'; @Controller('auth') export class AuthController { constructor(private readonly authService: AuthService) {} @Post('cadastro') cadastrar(@Body() dto: CadastroDto, @Req() req: Request) { return this.authService.cadastrar(dto, this.meta(req)); } @Post('login') login(@Body() dto: LoginDto, @Req() req: Request) { return this.authService.login(dto, this.meta(req)); } @Post('logout') logout(@Req() req: Request) { return this.authService.logout(this.authService.extrairBearer(req.headers.authorization)); } @Post('recuperar-senha') recuperarSenha(@Body() dto: RecuperarSenhaDto) { return this.authService.recuperarSenha(dto.email); } @Post('redefinir-senha') redefinirSenha(@Body() dto: RedefinirSenhaDto) { return this.authService.redefinirSenha(dto); } @Post('oauth/google') oauthGoogle(@Body() _dto: OAuthGoogleDto) { return { ok: false, message: 'Login Google ainda nao configurado. Integre a verificacao do idToken para ativar.', }; } @Get('me') @UseGuards(AuthGuard) me(@Req() req: RequestWithUser) { return this.authService.me(req.user!.id); } private meta(req: Request) { const userAgent = req.headers['user-agent']; return { ip: req.ip, userAgent: Array.isArray(userAgent) ? userAgent[0] : userAgent, }; } }