Update exception handling (#146)

This commit is contained in:
Aaron Po
2026-02-12 21:06:07 -05:00
committed by GitHub
parent 584fe6282f
commit 7129e5679e
28 changed files with 191 additions and 126 deletions

View File

@@ -5,5 +5,5 @@ namespace Service.Auth.Auth;
public interface ILoginService
{
Task<UserAccount?> LoginAsync(string username, string password);
Task<UserAccount> LoginAsync(string username, string password);
}

View File

@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Exceptions;
using Infrastructure.PasswordHashing;
using Infrastructure.Repository.Auth;
@@ -11,18 +12,24 @@ public class LoginService(
) : ILoginService
{
public async Task<UserAccount?> LoginAsync(string username, string password)
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;
if (user is null)
throw new UnauthorizedException("Invalid username or password.");
// @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;
if (activeCred is null)
throw new UnauthorizedException("Invalid username or password.");
if (!passwordInfrastructure.Verify(password, activeCred.Hash))
throw new UnauthorizedException("Invalid username or password.");
return user;
}
}

View File

@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Exceptions;
using Infrastructure.PasswordHashing;
using Infrastructure.Repository.Auth;
@@ -13,12 +14,16 @@ public class RegisterService(
public async Task<UserAccount> RegisterAsync(UserAccount userAccount, string password)
{
// Check if user already exists
var user = await authRepo.GetUserByUsernameAsync(userAccount.Username);
if (user is not null)
var existingUsername = await authRepo.GetUserByUsernameAsync(userAccount.Username);
var existingEmail = await authRepo.GetUserByEmailAsync(userAccount.Email);
if (existingUsername != null || existingEmail != null)
{
return null!;
throw new ConflictException("Username or email already exists");
}
// password hashing
var hashed = passwordInfrastructure.Hash(password);
@@ -32,5 +37,5 @@ public class RegisterService(
hashed);
}
}

View File

@@ -10,8 +10,10 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Domain\Domain.csproj" />
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
<ProjectReference Include="..\..\Domain.Entities\Domain.Entities.csproj" />
<ProjectReference Include="..\..\Domain.Exceptions\Domain.Exceptions.csproj" />
<ProjectReference
Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
<ProjectReference
Include="..\..\Infrastructure\Infrastructure.PasswordHashing\Infrastructure.PasswordHashing.csproj" />
</ItemGroup>