Migration to EF and starting to add Identity #11

Merged
jtom38 merged 16 commits from features/ef-identity-migration into main 2023-07-09 22:19:01 -07:00
6 changed files with 100 additions and 5 deletions
Showing only changes of commit fd60906a20 - Show all commits

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 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" /> <ProjectReference Include="..\Newsbot.Collector.Database\Newsbot.Collector.Database.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Controllers\v1\" />
</ItemGroup>
</Project> </Project>

View File

@ -49,6 +49,7 @@ builder.Services.AddScoped<IDiscordWebHooksRepository, DiscordWebhooksTable>();
builder.Services.AddScoped<IIconsRepository, IconsTable>(); builder.Services.AddScoped<IIconsRepository, IconsTable>();
builder.Services.AddScoped<ISourcesRepository, SourcesTable>(); builder.Services.AddScoped<ISourcesRepository, SourcesTable>();
builder.Services.AddScoped<IDiscordNotificationRepository, DiscordNotificationTable>(); builder.Services.AddScoped<IDiscordNotificationRepository, DiscordNotificationTable>();
builder.Services.AddScoped<IUserSourceSubscription, UserSourceSubscriptionTable>();
// Configure Identity // Configure Identity
builder.Services.AddScoped<IIdentityService, IdentityService>(); 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> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" /> <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" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup> </ItemGroup>