using System.Net.Http.Json;
using System.Text.Json;
public static class AuthHelper
{
///
/// Logs in as the given user and returns an HttpClient with the JWT
/// Authorization header pre-configured. Login also returns a refresh token;
/// integration tests use the access token only.
///
public static async Task LoginAsync(
ApiFixture fixture, string username = "entry1", string password = "password")
{
var client = fixture.CreateClient();
var loginResp = await client.PostAsJsonAsync("/api/v1/auth/login",
new { username, password });
loginResp.EnsureSuccessStatusCode();
var body = await loginResp.Content.ReadFromJsonAsync();
var token = body!.RootElement.GetProperty("data").GetProperty("token").GetString()!;
client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
return client;
}
}