using Femto.Api.Auth; using Femto.Api.Sessions; using Femto.Modules.Auth.Application; using Femto.Modules.Auth.Application.Commands.Login; using Femto.Modules.Auth.Application.Commands.Register; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace Femto.Api.Controllers.Auth; [ApiController] [Route("auth")] public class AuthController(IAuthModule authModule, IOptions cookieSettings) : ControllerBase { [HttpPost("login")] public async Task> Login([FromBody] LoginRequest request) { var result = await authModule.PostCommand( new LoginCommand(request.Username, request.Password) ); HttpContext.SetSession(result.Session, cookieSettings.Value); return new LoginResponse(result.UserId, result.Username); } [HttpPost("register")] public async Task> Register([FromBody] RegisterRequest request) { var result = await authModule.PostCommand( new RegisterCommand(request.Username, request.Password, request.SignupCode) ); HttpContext.SetSession(result.Session, cookieSettings.Value); return new RegisterResponse(result.UserId, result.Username); } [HttpDelete("session")] public async Task DeleteSession() { HttpContext.Response.Cookies.Delete("session"); return Ok(new { }); } }