Refactor auth/user services

This commit is contained in:
Aaron Po
2026-02-12 19:28:40 -05:00
parent 94061c6d84
commit d942d92db5
18 changed files with 98 additions and 55 deletions

View File

@@ -21,7 +21,8 @@
<ProjectReference Include="..\..\Domain\Domain.csproj" />
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Jwt\Infrastructure.Jwt.csproj" />
<ProjectReference Include="..\..\Service\Service.Core\Service.Core.csproj" />
<ProjectReference Include="..\..\Service\Service.Auth\Service.Auth.csproj" />
<ProjectReference Include="..\..\Service\Service.UserManagement\Service.UserManagement.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -3,20 +3,18 @@ using API.Core.Contracts.Common;
using Domain.Entities;
using Infrastructure.Jwt;
using Microsoft.AspNetCore.Mvc;
using Service.Core.Auth;
using Service.Auth.Auth;
namespace API.Core.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class AuthController(IAuthService auth, IJwtService jwtService) : ControllerBase
public class AuthController(IRegisterService register, ILoginService login, ITokenInfrastructure tokenInfrastructure) : ControllerBase
{
[HttpPost("register")]
public async Task<ActionResult<UserAccount>> Register([FromBody] RegisterRequest req)
{
var created = await auth.RegisterAsync(new UserAccount
var created = await register.RegisterAsync(new UserAccount
{
UserAccountId = Guid.Empty,
Username = req.Username,
@@ -27,8 +25,7 @@ namespace API.Core.Controllers
}, req.Password);
var jwtExpiresAt = DateTime.UtcNow.AddHours(1);
var jwt = jwtService.GenerateJwt(created.UserAccountId, created.Username, jwtExpiresAt
var jwt = tokenInfrastructure.GenerateJwt(created.UserAccountId, created.Username, jwtExpiresAt
);
var response = new ResponseBody<AuthPayload>
@@ -46,7 +43,7 @@ namespace API.Core.Controllers
[HttpPost("login")]
public async Task<ActionResult> Login([FromBody] LoginRequest req)
{
var userAccount = await auth.LoginAsync(req.Username, req.Password);
var userAccount = await login.LoginAsync(req.Username, req.Password);
if (userAccount is null)
{
return Unauthorized(new ResponseBody
@@ -58,7 +55,7 @@ namespace API.Core.Controllers
UserDTO dto = new(userAccount.UserAccountId, userAccount.Username);
var jwtExpiresAt = DateTime.UtcNow.AddHours(1);
var jwt = jwtService.GenerateJwt(userAccount.UserAccountId, userAccount.Username, jwtExpiresAt);
var jwt = tokenInfrastructure.GenerateJwt(userAccount.UserAccountId, userAccount.Username, jwtExpiresAt);
return Ok(new ResponseBody<AuthPayload>
{
@@ -67,4 +64,4 @@ namespace API.Core.Controllers
});
}
}
}
}

View File

@@ -1,6 +1,6 @@
using Domain.Entities;
using Microsoft.AspNetCore.Mvc;
using Service.Core.User;
using Service.UserManagement.User;
namespace API.Core.Controllers
{

View File

@@ -6,8 +6,9 @@ using Infrastructure.Repository.Auth;
using Infrastructure.Repository.Sql;
using Infrastructure.Repository.UserAccount;
using Microsoft.AspNetCore.Mvc;
using Service.Core.Auth;
using Service.Core.User;
using Service.Auth.Auth;
using Service.UserManagement.User;
var builder = WebApplication.CreateBuilder(args);
@@ -52,14 +53,20 @@ if (!builder.Environment.IsProduction())
builder.Logging.AddDebug();
}
// Dependency Injection
// Configure Dependency Injection -------------------------------------------------------------------------------------
builder.Services.AddSingleton<ISqlConnectionFactory, DefaultSqlConnectionFactory>();
builder.Services.AddScoped<IUserAccountRepository, UserAccountRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IAuthRepository, AuthRepository>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IJwtService, JwtService>();
builder.Services.AddScoped<IPasswordInfra, Argon2Infrastructure>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ILoginService, LoginService>();
builder.Services.AddScoped<IRegisterService, RegisterService>();
builder.Services.AddScoped<ITokenInfrastructure, JwtInfrastructure>();
builder.Services.AddScoped<IPasswordInfrastructure, Argon2Infrastructure>();
var app = builder.Build();