This commit is contained in:
john 2025-06-16 21:11:40 +02:00
parent e282e2ece3
commit 84457413b2
20 changed files with 224 additions and 246 deletions

View file

@ -1,24 +1,16 @@
using Femto.Api.Auth;
using Femto.Api.Sessions;
using Femto.Common;
using Femto.Modules.Auth.Application.Interface.CreateSignupCode;
using Femto.Modules.Auth.Application.Interface.GetSignupCodesQuery;
using Femto.Modules.Auth.Application.Interface.Register;
using Femto.Modules.Auth.Application.Services;
using Femto.Modules.Auth.Contracts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace Femto.Api.Controllers.Auth;
[ApiController]
[Route("auth")]
public class AuthController(
IAuthModule authModule,
IOptions<CookieSettings> cookieSettings,
ICurrentUserContext currentUserContext,
ILogger<AuthController> logger,
IAuthService authService
) : ControllerBase
{
@ -28,17 +20,17 @@ public class AuthController(
CancellationToken cancellationToken
)
{
var user = await authService.GetUserWithCredentials(
var result = await authService.GetUserWithCredentials(
request.Username,
request.Password,
cancellationToken
);
if (user is null)
if (result is null)
return this.BadRequest();
var session = await authService.CreateStrongSession(user.Id);
var (user, session) = result;
HttpContext.SetSession(session, user);
return new LoginResponse(user.Id, user.Username, user.Roles.Any(r => r == Role.SuperUser));
@ -47,13 +39,10 @@ public class AuthController(
[HttpPost("register")]
public async Task<ActionResult<RegisterResponse>> Register([FromBody] RegisterRequest request)
{
var user = await authModule.Command(
new RegisterCommand(request.Username, request.Password, request.SignupCode)
);
var session = await authService.CreateStrongSession(user.Id);
var (user, session) = await authService.CreateUserWithCredentials(request.Username, request.Password, request.SignupCode);
HttpContext.SetSession(session, user);
return new RegisterResponse(
user.Id,
user.Username,
@ -106,10 +95,7 @@ public class AuthController(
CancellationToken cancellationToken
)
{
await authModule.Command(
new CreateSignupCodeCommand(request.Code, request.Email, request.Name),
cancellationToken
);
await authService.AddSignupCode(request.Code, request.Name, cancellationToken);
return Ok(new { });
}
@ -120,7 +106,7 @@ public class AuthController(
CancellationToken cancellationToken
)
{
var codes = await authModule.Query(new GetSignupCodesQuery(), cancellationToken);
var codes = await authService.GetSignupCodes(cancellationToken);
return new ListSignupCodesResult(
codes.Select(c => new SignupCodeDto(