You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthenticationsController.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. namespace Diligent.WebAPI.Host.Controllers.V1
  2. {
  3. [ApiVersion("1.0")]
  4. [Route("v{version:apiVersion}/authentications")]
  5. [ApiController]
  6. public class AuthenticationsController : ControllerBase
  7. {
  8. private readonly IAuthenticationService _service;
  9. public AuthenticationsController(IAuthenticationService service)
  10. {
  11. _service = service;
  12. }
  13. [HttpGet("ForgotPassword")]
  14. public async Task<IActionResult> ForgotPassword(string email)
  15. {
  16. var response = await _service.GetEmailConfirmationUrlAsync(email);
  17. if (response.IsError is true)
  18. return BadRequest(new { message = response.ErrorMessage });
  19. return Ok(response.Data);
  20. }
  21. [HttpPost("RessetPassword")]
  22. public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordModel model)
  23. {
  24. var response = await _service.PasswordResetAsync(email: model.Email, code: model.Code, password: model.Password);
  25. if (response.IsError is true)
  26. return BadRequest(new { message = response.ErrorMessage });
  27. return Ok(response.Data);
  28. }
  29. [HttpPost("authenticate")]
  30. public async Task<IActionResult> Authenticate([FromBody] AuthenticateRequestDto model)
  31. {
  32. var response = await _service.Authenticate(model);
  33. if (response.IsError is true)
  34. return BadRequest(new { message = response.ErrorMessage });
  35. return Ok(response.Data);
  36. }
  37. [HttpPost("refresh")]
  38. public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenRequestDto model)
  39. {
  40. var response = await _service.RefreshTokenAsync(model);
  41. if (response.Error != null)
  42. {
  43. return BadRequest(new AuthFailedResponse { Error = response.Error });
  44. }
  45. return Ok(response);
  46. }
  47. [HttpPost("logout")]
  48. public async Task<IActionResult> Logout(int userId)
  49. {
  50. var response = await _service.DeleteRefreshToken(userId);
  51. if (response.IsError)
  52. {
  53. return BadRequest(new { message = response.ErrorMessage });
  54. }
  55. return Ok();
  56. }
  57. [HttpPost("authenticateGoogle")]
  58. public async Task<IActionResult> GoogleLogin(GoogleApiModel model)
  59. {
  60. var response = await _service.Authenticate(model);
  61. if (response.IsError is true)
  62. return BadRequest(new { message = response.ErrorMessage });
  63. return Ok(response.Data);
  64. }
  65. }
  66. }