mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-04-06 02:19:05 +00:00
Refactor auth/user services
This commit is contained in:
@@ -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>
|
||||
|
||||
@@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using Domain.Entities;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Service.Core.User;
|
||||
using Service.UserManagement.User;
|
||||
|
||||
namespace API.Core.Controllers
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user