Refactor auth/user services

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

View File

@@ -0,0 +1,9 @@
using System.Threading.Tasks;
using Domain.Entities;
namespace Service.Auth.Auth;
public interface ILoginService
{
Task<UserAccount?> LoginAsync(string username, string password);
}

View File

@@ -0,0 +1,9 @@
using System.Threading.Tasks;
using Domain.Entities;
namespace Service.Auth.Auth;
public interface IRegisterService
{
Task<UserAccount> RegisterAsync(UserAccount userAccount, string password);
}

View File

@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Domain.Entities;
using Infrastructure.PasswordHashing;
using Infrastructure.Repository.Auth;
namespace Service.Auth.Auth;
public class LoginService(
IAuthRepository authRepo,
IPasswordInfrastructure passwordInfrastructure
) : ILoginService
{
public async Task<UserAccount?> LoginAsync(string username, string password)
{
// Attempt lookup by username
var user = await authRepo.GetUserByUsernameAsync(username);
// the user was not found
if (user is null) return null;
// @todo handle expired passwords
var activeCred = await authRepo.GetActiveCredentialByUserAccountIdAsync(user.UserAccountId);
if (activeCred is null) return null;
return !passwordInfrastructure.Verify(password, activeCred.Hash) ? null : user;
}
}

View File

@@ -1,13 +1,14 @@
using System.Threading.Tasks;
using Domain.Entities;
using Infrastructure.PasswordHashing;
using Infrastructure.Repository.Auth;
namespace Service.Core.Auth;
namespace Service.Auth.Auth;
public class AuthService(
public class RegisterService(
IAuthRepository authRepo,
IPasswordInfra passwordInfra
) : IAuthService
IPasswordInfrastructure passwordInfrastructure
) : IRegisterService
{
public async Task<UserAccount> RegisterAsync(UserAccount userAccount, string password)
{
@@ -19,7 +20,7 @@ public class AuthService(
}
// password hashing
var hashed = passwordInfra.Hash(password);
var hashed = passwordInfrastructure.Hash(password);
// Register user with hashed password
return await authRepo.RegisterUserAsync(
@@ -31,18 +32,5 @@ public class AuthService(
hashed);
}
public async Task<UserAccount?> LoginAsync(string username, string password)
{
// Attempt lookup by username
var user = await authRepo.GetUserByUsernameAsync(username);
// the user was not found
if (user is null) return null;
// @todo handle expired passwords
var activeCred = await authRepo.GetActiveCredentialByUserAccountIdAsync(user.UserAccountId);
if (activeCred is null) return null;
return !passwordInfra.Verify(password, activeCred.Hash) ? null : user;
}
}

View File

@@ -3,7 +3,6 @@
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Service.Core</RootNamespace>
</PropertyGroup>
<ItemGroup>

View File

@@ -1,9 +0,0 @@
using Domain.Entities;
namespace Service.Core.Auth;
public interface IAuthService
{
Task<UserAccount> RegisterAsync(UserAccount userAccount, string password);
Task<UserAccount?> LoginAsync(string username, string password);
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
</ItemGroup>
</Project>

View File

@@ -1,6 +1,6 @@
using Domain.Entities;
namespace Service.Core.User;
namespace Service.UserManagement.User;
public interface IUserService
{

View File

@@ -1,7 +1,7 @@
using Domain.Entities;
using Infrastructure.Repository.UserAccount;
namespace Service.Core.User;
namespace Service.UserManagement.User;
public class UserService(IUserAccountRepository repository) : IUserService
{