femto-backend/Femto.Api/Controllers/Auth/AuthController.cs
2025-05-18 19:11:43 +02:00

45 lines
1.4 KiB
C#

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> cookieSettings) : ControllerBase
{
[HttpPost("login")]
public async Task<ActionResult<LoginResponse>> 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<ActionResult<RegisterResponse>> 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<ActionResult> DeleteSession()
{
HttpContext.Response.Cookies.Delete("session");
return Ok(new { });
}
}