mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Refactor auth/user services
This commit is contained in:
9
src/Core/Service/Service.Auth/Auth/ILoginService.cs
Normal file
9
src/Core/Service/Service.Auth/Auth/ILoginService.cs
Normal 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);
|
||||
}
|
||||
9
src/Core/Service/Service.Auth/Auth/IRegisterService.cs
Normal file
9
src/Core/Service/Service.Auth/Auth/IRegisterService.cs
Normal 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);
|
||||
}
|
||||
28
src/Core/Service/Service.Auth/Auth/LoginService.cs
Normal file
28
src/Core/Service/Service.Auth/Auth/LoginService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>Service.Core</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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>
|
||||
@@ -1,6 +1,6 @@
|
||||
using Domain.Entities;
|
||||
|
||||
namespace Service.Core.User;
|
||||
namespace Service.UserManagement.User;
|
||||
|
||||
public interface IUserService
|
||||
{
|
||||
@@ -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
|
||||
{
|
||||
Reference in New Issue
Block a user