Add sender login functionality to AuthService

Added a `SenderLoginResult` enum to represent outcomes of sender login attempts.
Implemented the `LoginSenderAsync` method in `AuthService` to authenticate sender users via the `/api/auth?cookie=true` endpoint.
The method handles HTTP response codes and returns appropriate `SenderLoginResult` values.
Included the `System.Net.Http.Json` namespace to support JSON-based HTTP requests.
This commit is contained in:
2026-06-11 10:01:33 +02:00
parent 14aff03de4
commit e98e18cfe0

View File

@@ -1,4 +1,5 @@
using System.Net; using System.Net;
using System.Net.Http.Json;
using EnvelopeGenerator.ReceiverUI.Options; using EnvelopeGenerator.ReceiverUI.Options;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@@ -6,6 +7,8 @@ namespace EnvelopeGenerator.ReceiverUI.Services;
public enum EnvelopeLoginResult { Success, InvalidCode, NotFound, Error } public enum EnvelopeLoginResult { Success, InvalidCode, NotFound, Error }
public enum SenderLoginResult { Success, InvalidCredentials, Error }
public class AuthService(HttpClient http, IOptions<ApiOptions> apiOptions) public class AuthService(HttpClient http, IOptions<ApiOptions> apiOptions)
{ {
private readonly ApiOptions _api = apiOptions.Value; private readonly ApiOptions _api = apiOptions.Value;
@@ -54,4 +57,25 @@ public class AuthService(HttpClient http, IOptions<ApiOptions> apiOptions)
null, cancel); null, cancel);
return response.IsSuccessStatusCode; return response.IsSuccessStatusCode;
} }
/// <summary>
/// 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.
/// </summary>
public async Task<SenderLoginResult> 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
};
}
} }