File size: 1,680 Bytes
e3bc69d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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,
    };
  }
}