diff --git a/EnvelopeGenerator.ReceiverUI/Services/AuthService.cs b/EnvelopeGenerator.ReceiverUI/Services/AuthService.cs index b68097b3..c0b020dd 100644 --- a/EnvelopeGenerator.ReceiverUI/Services/AuthService.cs +++ b/EnvelopeGenerator.ReceiverUI/Services/AuthService.cs @@ -1,4 +1,5 @@ using System.Net; +using System.Net.Http.Json; using EnvelopeGenerator.ReceiverUI.Options; using Microsoft.Extensions.Options; @@ -6,6 +7,8 @@ namespace EnvelopeGenerator.ReceiverUI.Services; public enum EnvelopeLoginResult { Success, InvalidCode, NotFound, Error } +public enum SenderLoginResult { Success, InvalidCredentials, Error } + public class AuthService(HttpClient http, IOptions apiOptions) { private readonly ApiOptions _api = apiOptions.Value; @@ -54,4 +57,25 @@ public class AuthService(HttpClient http, IOptions apiOptions) null, cancel); return response.IsSuccessStatusCode; } + + /// + /// Authenticates a sender user with username and password. + /// Calls POST /api/auth?cookie=true with JSON body. + /// On success the API sets an authentication cookie automatically. + /// + public async Task LoginSenderAsync(string username, string password, CancellationToken cancel = default) + { + var requestBody = new { username, password }; + + var response = await http.PostAsJsonAsync( + $"{_api.BaseUrl}/api/auth?cookie=true", + requestBody, cancel); + + return response.StatusCode switch + { + HttpStatusCode.OK => SenderLoginResult.Success, + HttpStatusCode.Unauthorized => SenderLoginResult.InvalidCredentials, + _ => SenderLoginResult.Error + }; + } }