2023-07-09 22:11:30 -07:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Newsbot.Collector.Api.Domain.Requests;
|
|
|
|
using Newsbot.Collector.Api.Domain.Response;
|
2023-07-10 22:41:39 -07:00
|
|
|
using Newsbot.Collector.Api.Domain.Results;
|
2023-07-09 22:11:30 -07:00
|
|
|
using Newsbot.Collector.Api.Services;
|
|
|
|
using Newsbot.Collector.Domain.Dto;
|
|
|
|
using Newsbot.Collector.Domain.Entities;
|
|
|
|
|
|
|
|
namespace Newsbot.Collector.Api.Controllers;
|
|
|
|
|
|
|
|
[ApiController]
|
|
|
|
[Route("/api/account")]
|
|
|
|
public class AccountController : ControllerBase
|
|
|
|
{
|
|
|
|
private IIdentityService _identityService;
|
|
|
|
|
|
|
|
public AccountController(IIdentityService identityService)
|
|
|
|
{
|
|
|
|
_identityService = identityService;
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("register")]
|
|
|
|
public IActionResult Register([FromBody] RegisterUserRequest user)
|
|
|
|
{
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return new BadRequestObjectResult(new AuthFailedResponse
|
|
|
|
{
|
|
|
|
Errors = ModelState.Values
|
|
|
|
.Select(x => x.Errors
|
|
|
|
.Select(y => y.ErrorMessage).FirstOrDefault())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.Email is null)
|
|
|
|
{
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user.Password is null)
|
|
|
|
{
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = _identityService.Register(user.Email, user.Password);
|
2023-07-10 22:41:39 -07:00
|
|
|
return CheckIfSuccessful(response);
|
2023-07-09 22:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[HttpPost("login")]
|
|
|
|
public IActionResult Login([FromBody] UserLoginRequest request)
|
|
|
|
{
|
|
|
|
if (request.Email is null)
|
|
|
|
{
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.Password is null)
|
|
|
|
{
|
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
var response = _identityService.Login(request.Email, request.Password);
|
2023-07-10 22:41:39 -07:00
|
|
|
return CheckIfSuccessful(response);
|
|
|
|
}
|
2023-07-09 22:11:30 -07:00
|
|
|
|
2023-07-10 22:41:39 -07:00
|
|
|
[HttpPost("refresh")]
|
|
|
|
public ActionResult RefreshToken([FromBody] UserRefreshTokenRequest request)
|
|
|
|
{
|
|
|
|
var response = _identityService.RefreshToken(request.Token ?? "", request.RefreshToken ?? "");
|
|
|
|
return CheckIfSuccessful(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ActionResult CheckIfSuccessful(AuthenticationResult result)
|
|
|
|
{
|
|
|
|
if (!result.IsSuccessful)
|
2023-07-09 22:11:30 -07:00
|
|
|
{
|
|
|
|
return new BadRequestObjectResult( new AuthFailedResponse
|
|
|
|
{
|
2023-07-10 22:41:39 -07:00
|
|
|
Errors = result.ErrorMessage
|
2023-07-09 22:11:30 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OkObjectResult(new AuthSuccessfulResponse
|
|
|
|
{
|
2023-07-10 22:41:39 -07:00
|
|
|
Token = result.Token,
|
|
|
|
RefreshToken = result.RefreshToken
|
2023-07-09 22:11:30 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|