Adding the auth layers, not 100% finished yet

This commit is contained in:
James Tombleson 2023-07-09 22:11:30 -07:00
parent d28ad7424f
commit fd60906a20
6 changed files with 100 additions and 5 deletions

View File

@ -0,0 +1,89 @@
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
});
}
}

View File

@ -2,5 +2,5 @@ namespace Newsbot.Collector.Api.Domain.Response;
public class AuthFailedResponse
{
public IEnumerable<string>? Errors { get; set; }
public IEnumerable<string?>? Errors { get; set; }
}

View File

@ -37,8 +37,4 @@
<ProjectReference Include="..\Newsbot.Collector.Database\Newsbot.Collector.Database.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\v1\" />
</ItemGroup>
</Project>

View File

@ -49,6 +49,7 @@ builder.Services.AddScoped<IDiscordWebHooksRepository, DiscordWebhooksTable>();
builder.Services.AddScoped<IIconsRepository, IconsTable>();
builder.Services.AddScoped<ISourcesRepository, SourcesTable>();
builder.Services.AddScoped<IDiscordNotificationRepository, DiscordNotificationTable>();
builder.Services.AddScoped<IUserSourceSubscription, UserSourceSubscriptionTable>();
// Configure Identity
builder.Services.AddScoped<IIdentityService, IdentityService>();

View File

@ -0,0 +1,7 @@
namespace Newsbot.Collector.Domain.Dto;
public class UserNewDto
{
public string? Username { get; set; }
public string? Password { get; set; }
}

View File

@ -7,7 +7,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="7.0.8" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>