2023-07-14 22:24:32 -07:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-07-09 22:11:30 -07:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-07-14 22:24:32 -07:00
|
|
|
using Newsbot.Collector.Api.Domain;
|
2023-07-23 22:57:54 -07:00
|
|
|
using Newsbot.Collector.Domain.Requests;
|
|
|
|
using Newsbot.Collector.Domain.Response;
|
|
|
|
using Newsbot.Collector.Domain.Results;
|
|
|
|
using Newsbot.Collector.Services;
|
2023-07-09 22:11:30 -07:00
|
|
|
|
2023-07-23 16:22:49 -07:00
|
|
|
namespace Newsbot.Collector.Api.Controllers.v1;
|
2023-07-09 22:11:30 -07:00
|
|
|
|
|
|
|
[ApiController]
|
2023-07-23 16:22:49 -07:00
|
|
|
[Route("/api/v1/account")]
|
|
|
|
public class IdentityController : ControllerBase
|
2023-07-09 22:11:30 -07:00
|
|
|
{
|
2023-07-23 22:57:54 -07:00
|
|
|
private readonly ILogger<IdentityController> _logger;
|
|
|
|
private readonly IIdentityService _identityService;
|
2023-07-09 22:11:30 -07:00
|
|
|
|
2023-07-23 22:57:54 -07:00
|
|
|
public IdentityController(IIdentityService identityService, ILogger<IdentityController> logger)
|
2023-07-09 22:11:30 -07:00
|
|
|
{
|
|
|
|
_identityService = identityService;
|
2023-07-23 22:57:54 -07:00
|
|
|
_logger = logger;
|
2023-07-09 22:11:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
[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);
|
|
|
|
}
|
|
|
|
|
2023-07-14 22:24:32 -07:00
|
|
|
[HttpPost("addRole")]
|
2023-07-23 16:22:49 -07:00
|
|
|
[Authorize(Roles = Authorization.AdministratorsRole)]
|
2023-07-14 22:24:32 -07:00
|
|
|
public ActionResult AddRole([FromBody] AddRoleRequest request)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_identityService.AddRole(request.RoleName ?? "", request.UserId ?? "");
|
|
|
|
return new OkResult();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2023-07-23 22:57:54 -07:00
|
|
|
_logger.LogWarning(ex, "Failed to add role to user");
|
2023-07-14 22:24:32 -07:00
|
|
|
return new BadRequestResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:41:39 -07:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|