From dac3acfecfff94fa1aef08cf009b815ddd809e0b Mon Sep 17 00:00:00 2001 From: john Date: Mon, 16 Jun 2025 21:24:37 +0200 Subject: [PATCH] add remember me to API --- Femto.Api/Controllers/Auth/AuthController.cs | 22 +++++++++++-------- Femto.Api/Controllers/Auth/LoginRequest.cs | 2 +- Femto.Api/Controllers/Auth/RegisterRequest.cs | 2 +- .../Application/Services/AuthService.cs | 14 +++++------- .../Application/Services/IAuthService.cs | 16 +++++++++----- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/Femto.Api/Controllers/Auth/AuthController.cs b/Femto.Api/Controllers/Auth/AuthController.cs index a91d906..b3ad33f 100644 --- a/Femto.Api/Controllers/Auth/AuthController.cs +++ b/Femto.Api/Controllers/Auth/AuthController.cs @@ -9,10 +9,8 @@ namespace Femto.Api.Controllers.Auth; [ApiController] [Route("auth")] -public class AuthController( - ICurrentUserContext currentUserContext, - IAuthService authService -) : ControllerBase +public class AuthController(ICurrentUserContext currentUserContext, IAuthService authService) + : ControllerBase { [HttpPost("login")] public async Task> Login( @@ -23,14 +21,15 @@ public class AuthController( var result = await authService.GetUserWithCredentials( request.Username, request.Password, + request.RememberMe, cancellationToken ); - + if (result is null) return this.BadRequest(); - + var (user, session) = result; - + HttpContext.SetSession(session, user); return new LoginResponse(user.Id, user.Username, user.Roles.Any(r => r == Role.SuperUser)); @@ -39,10 +38,15 @@ public class AuthController( [HttpPost("register")] public async Task> Register([FromBody] RegisterRequest request) { - var (user, session) = await authService.CreateUserWithCredentials(request.Username, request.Password, request.SignupCode); + var (user, session) = await authService.CreateUserWithCredentials( + request.Username, + request.Password, + request.SignupCode, + request.RememberMe + ); HttpContext.SetSession(session, user); - + return new RegisterResponse( user.Id, user.Username, diff --git a/Femto.Api/Controllers/Auth/LoginRequest.cs b/Femto.Api/Controllers/Auth/LoginRequest.cs index 8366d14..6c09e64 100644 --- a/Femto.Api/Controllers/Auth/LoginRequest.cs +++ b/Femto.Api/Controllers/Auth/LoginRequest.cs @@ -1,3 +1,3 @@ namespace Femto.Api.Controllers.Auth; -public record LoginRequest(string Username, string Password); \ No newline at end of file +public record LoginRequest(string Username, string Password, bool RememberMe); \ No newline at end of file diff --git a/Femto.Api/Controllers/Auth/RegisterRequest.cs b/Femto.Api/Controllers/Auth/RegisterRequest.cs index f386198..ee21297 100644 --- a/Femto.Api/Controllers/Auth/RegisterRequest.cs +++ b/Femto.Api/Controllers/Auth/RegisterRequest.cs @@ -1,3 +1,3 @@ namespace Femto.Api.Controllers.Auth; -public record RegisterRequest(string Username, string Password, string SignupCode, string? Email); \ No newline at end of file +public record RegisterRequest(string Username, string Password, string SignupCode, bool RememberMe); \ No newline at end of file diff --git a/Femto.Modules.Auth/Application/Services/AuthService.cs b/Femto.Modules.Auth/Application/Services/AuthService.cs index 0a73d60..4c741fc 100644 --- a/Femto.Modules.Auth/Application/Services/AuthService.cs +++ b/Femto.Modules.Auth/Application/Services/AuthService.cs @@ -15,11 +15,10 @@ internal class AuthService( IDbConnectionFactory connectionFactory ) : IAuthService { - public async Task GetUserWithCredentials( - string username, + public async Task GetUserWithCredentials(string username, string password, - CancellationToken cancellationToken = default - ) + bool createLongTermSession, + CancellationToken cancellationToken = default) { var user = await context .Users.Where(u => u.Username == username) @@ -77,12 +76,11 @@ internal class AuthService( await storage.DeleteSession(sessionId); } - public async Task CreateUserWithCredentials( - string username, + public async Task CreateUserWithCredentials(string username, string password, string signupCode, - CancellationToken cancellationToken = default - ) + bool createLongTermSession, + CancellationToken cancellationToken = default) { var now = DateTimeOffset.UtcNow; diff --git a/Femto.Modules.Auth/Application/Services/IAuthService.cs b/Femto.Modules.Auth/Application/Services/IAuthService.cs index f07939e..c8c252d 100644 --- a/Femto.Modules.Auth/Application/Services/IAuthService.cs +++ b/Femto.Modules.Auth/Application/Services/IAuthService.cs @@ -14,6 +14,7 @@ public interface IAuthService public Task GetUserWithCredentials( string username, string password, + bool createLongTermSession, CancellationToken cancellationToken = default ); public Task GetUserWithId( @@ -25,16 +26,21 @@ public interface IAuthService public Task GetSession(string sessionId); public Task DeleteSession(string sessionId); - public Task CreateUserWithCredentials( - string username, + public Task CreateUserWithCredentials(string username, string password, string signupCode, + bool createLongTermSession, + CancellationToken cancellationToken = default); + + public Task AddSignupCode( + string code, + string recipientName, CancellationToken cancellationToken = default ); - public Task AddSignupCode(string code, string recipientName, CancellationToken cancellationToken = default); - - public Task> GetSignupCodes(CancellationToken cancellationToken = default); + public Task> GetSignupCodes( + CancellationToken cancellationToken = default + ); } public record UserAndSession(UserInfo User, Session Session);