Spaces:
Sleeping
Sleeping
| 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'; | |
| ('auth') | |
| export class AuthController { | |
| constructor(private readonly authService: AuthService) {} | |
| ('cadastro') | |
| cadastrar(() dto: CadastroDto, () req: Request) { | |
| return this.authService.cadastrar(dto, this.meta(req)); | |
| } | |
| ('login') | |
| login(() dto: LoginDto, () req: Request) { | |
| return this.authService.login(dto, this.meta(req)); | |
| } | |
| ('logout') | |
| logout(() req: Request) { | |
| return this.authService.logout(this.authService.extrairBearer(req.headers.authorization)); | |
| } | |
| ('recuperar-senha') | |
| recuperarSenha(() dto: RecuperarSenhaDto) { | |
| return this.authService.recuperarSenha(dto.email); | |
| } | |
| ('redefinir-senha') | |
| redefinirSenha(() dto: RedefinirSenhaDto) { | |
| return this.authService.redefinirSenha(dto); | |
| } | |
| ('oauth/google') | |
| oauthGoogle(() _dto: OAuthGoogleDto) { | |
| return { | |
| ok: false, | |
| message: 'Login Google ainda nao configurado. Integre a verificacao do idToken para ativar.', | |
| }; | |
| } | |
| ('me') | |
| (AuthGuard) | |
| me(() 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, | |
| }; | |
| } | |
| } | |