| | 1 | | using Despesas.Application.Abstractions; |
| | 2 | | using Despesas.Application.Dtos; |
| | 3 | | using Despesas.Application.Dtos.Core; |
| | 4 | | using Despesas.Backend.Controllers.Abstractions; |
| | 5 | | using Despesas.GlobalException.CustomExceptions.Acesso; |
| | 6 | | using Microsoft.AspNetCore.Authorization; |
| | 7 | | using Microsoft.AspNetCore.Mvc; |
| | 8 | |
|
| | 9 | | namespace Despesas.Backend.Controllers; |
| | 10 | |
|
| | 11 | | [Route("api/[controller]")] |
| | 12 | | public class AcessoController : UnitControllerBase |
| | 13 | | { |
| | 14 | | private readonly IAcessoBusiness<AcessoDto, LoginDto> _acessoBusiness; |
| | 15 | |
|
| 18 | 16 | | public AcessoController(IAcessoBusiness<AcessoDto, LoginDto> acessoBusiness) |
| 18 | 17 | | { |
| 18 | 18 | | _acessoBusiness = acessoBusiness; |
| 18 | 19 | | } |
| | 20 | |
|
| | 21 | | [AllowAnonymous] |
| | 22 | | [HttpPost] |
| | 23 | | [ProducesResponseType(200, Type = typeof(bool))] |
| | 24 | | public async Task<IActionResult> Post([FromBody] AcessoDto acessoDto) |
| 8 | 25 | | { |
| 8 | 26 | | await _acessoBusiness.Create(acessoDto); |
| 1 | 27 | | return Ok(true); |
| 1 | 28 | | } |
| | 29 | |
|
| | 30 | | [AllowAnonymous] |
| | 31 | | [HttpPost("SignIn")] |
| | 32 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 33 | | [ProducesResponseType(400, Type = typeof(string))] |
| | 34 | | [ProducesResponseType(401)] |
| | 35 | | public async Task<IActionResult> SignIn([FromBody] LoginDto login) |
| 4 | 36 | | { |
| 4 | 37 | | var result = await _acessoBusiness.ValidateCredentials(login); |
| 1 | 38 | | return Ok(result); |
| 1 | 39 | | } |
| | 40 | |
|
| | 41 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | 42 | | [AllowAnonymous] |
| | 43 | | [HttpPost("SignInWithGoogle")] |
| | 44 | | public async Task<IActionResult> GoogleSignIn([FromBody] GoogleAuthenticationDto authentication) |
| 0 | 45 | | { |
| 0 | 46 | | if (!authentication.Authenticated) |
| 0 | 47 | | throw new ArgumentException("Erro ao autenticar com o Google."); |
| | 48 | |
|
| 0 | 49 | | var authResult = await _acessoBusiness.ValidateExternalCredentials(authentication); |
| 0 | 50 | | return Ok(authResult); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | 54 | | [HttpPost("ChangePassword")] |
| | 55 | | [Authorize("Bearer", Roles = "User, Admin")] |
| | 56 | | public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordDto changePasswordDto) |
| 2 | 57 | | { |
| 2 | 58 | | if (changePasswordDto == null) |
| 1 | 59 | | throw new TrocaSenhaException(); |
| | 60 | |
|
| 1 | 61 | | await _acessoBusiness.ChangePassword(UserIdentity, changePasswordDto.Senha ?? ""); |
| 1 | 62 | | return Ok(true); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | 66 | | [HttpPost("RecoveryPassword")] |
| | 67 | | [AllowAnonymous] |
| | 68 | | public async Task<IActionResult> RecoveryPassword([FromBody] string email) |
| 2 | 69 | | { |
| 2 | 70 | | await _acessoBusiness.RecoveryPassword(email); |
| 1 | 71 | | return Ok(true); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | [ApiExplorerSettings(IgnoreApi = true)] |
| | 75 | | [HttpGet("refreshToken/{refreshToken}")] |
| | 76 | | [AllowAnonymous] |
| | 77 | | public async Task<IActionResult> Refresh([FromRoute] string refreshToken) |
| 2 | 78 | | { |
| 2 | 79 | | var result = await _acessoBusiness.ValidateCredentials(refreshToken); |
| 1 | 80 | | return Ok(result); |
| 1 | 81 | | } |
| | 82 | | } |