mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 18:52:06 +00:00
Update exception handling (#146)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
|
||||
<ProjectReference Include="..\..\Domain.Exceptions\Domain.Exceptions.csproj" />
|
||||
<ProjectReference
|
||||
Include="..\..\Infrastructure\Infrastructure.Repository\Infrastructure.Repository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Service.UserManagement.User;
|
||||
public interface IUserService
|
||||
{
|
||||
Task<IEnumerable<UserAccount>> GetAllAsync(int? limit = null, int? offset = null);
|
||||
Task<UserAccount?> GetByIdAsync(Guid id);
|
||||
Task<UserAccount> GetByIdAsync(Guid id);
|
||||
|
||||
Task UpdateAsync(UserAccount userAccount);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Domain.Entities;
|
||||
using Domain.Exceptions;
|
||||
using Infrastructure.Repository.UserAccount;
|
||||
|
||||
namespace Service.UserManagement.User;
|
||||
@@ -10,9 +11,12 @@ public class UserService(IUserAccountRepository repository) : IUserService
|
||||
return await repository.GetAllAsync(limit, offset);
|
||||
}
|
||||
|
||||
public async Task<UserAccount?> GetByIdAsync(Guid id)
|
||||
public async Task<UserAccount> GetByIdAsync(Guid id)
|
||||
{
|
||||
return await repository.GetByIdAsync(id);
|
||||
var user = await repository.GetByIdAsync(id);
|
||||
if (user is null)
|
||||
throw new NotFoundException($"User with ID {id} not found");
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(UserAccount userAccount)
|
||||
|
||||
Reference in New Issue
Block a user