Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AuthenticationsController.cs 3.0KB

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