89 lines
2.3 KiB
C#
89 lines
2.3 KiB
C#
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Newsbot.Collector.Api.Domain.Requests;
|
|
using Newsbot.Collector.Api.Domain.Response;
|
|
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);
|
|
|
|
if (!response.IsSuccessful)
|
|
{
|
|
return new BadRequestObjectResult( new AuthFailedResponse
|
|
{
|
|
Errors = response.ErrorMessage
|
|
});
|
|
}
|
|
|
|
return new OkObjectResult(new AuthSuccessfulResponse
|
|
{
|
|
Token = response.Token
|
|
});
|
|
}
|
|
|
|
[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);
|
|
|
|
if (!response.IsSuccessful)
|
|
{
|
|
return new BadRequestObjectResult( new AuthFailedResponse
|
|
{
|
|
Errors = response.ErrorMessage
|
|
});
|
|
}
|
|
|
|
return new OkObjectResult(new AuthSuccessfulResponse
|
|
{
|
|
Token = response.Token
|
|
});
|
|
}
|
|
} |