Stránky na cestách z e-mailových odkazů API (/confirm-email, /reset-password). Token se spotřebuje až tlačítkem, ne otevřením odkazu (poštovní skenery). Profil s úpravou jména, telefonu a e-mailových upozornění, odhlášení ze všech zařízení. Chyby validace z API se zobrazují u polí formuláře. Refs #21
53 lines
2.4 KiB
C#
53 lines
2.4 KiB
C#
using System.Net;
|
||
using System.Net.Http.Json;
|
||
|
||
namespace Helpdesk.Web.Api;
|
||
|
||
/// <summary>
|
||
/// Anonymní endpointy <c>/api/v1/auth</c>. Záměrně bez <see cref="BearerTokenHandler"/>:
|
||
/// obnova tokenu běží uvnitř ověřování cookie a čtení tokenu z cookie by se zacyklilo.
|
||
/// </summary>
|
||
public sealed class AuthApiClient(HttpClient http)
|
||
{
|
||
public async Task<AuthResponse> LoginAsync(LoginRequest request, CancellationToken ct)
|
||
{
|
||
using var response = await http.PostAsJsonAsync("api/v1/auth/login", request, ct);
|
||
return await ApiResponse.ReadAsync<AuthResponse>(response, ct);
|
||
}
|
||
|
||
/// <summary>Registrace. Roli (student / vyučující) určí API podle domény e-mailu.</summary>
|
||
public async Task<UserDto> RegisterAsync(RegisterRequest request, CancellationToken ct)
|
||
{
|
||
using var response = await http.PostAsJsonAsync("api/v1/auth/register", request, ct);
|
||
return await ApiResponse.ReadAsync<UserDto>(response, ct);
|
||
}
|
||
|
||
public async Task ConfirmEmailAsync(string token, CancellationToken ct)
|
||
{
|
||
using var response = await http.PostAsJsonAsync("api/v1/auth/confirm-email", new EmailTokenRequest(token), ct);
|
||
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||
}
|
||
|
||
/// <summary>API odpoví stejně, ať účet existuje, nebo ne – stránka tak neprozradí registrované e-maily.</summary>
|
||
public async Task ForgotPasswordAsync(string email, CancellationToken ct)
|
||
{
|
||
using var response = await http.PostAsJsonAsync("api/v1/auth/forgot-password", new ForgotPasswordRequest(email), ct);
|
||
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||
}
|
||
|
||
public async Task ResetPasswordAsync(ResetPasswordRequest request, CancellationToken ct)
|
||
{
|
||
using var response = await http.PostAsJsonAsync("api/v1/auth/reset-password", request, ct);
|
||
await ApiResponse.EnsureSuccessAsync(response, ct);
|
||
}
|
||
|
||
/// <summary>Obnoví pár tokenů. Neplatný nebo už použitý refresh token → <c>null</c>.</summary>
|
||
public async Task<TokenSet?> RefreshAsync(string refreshToken, CancellationToken ct)
|
||
{
|
||
using var response = await http.PostAsJsonAsync("api/v1/auth/refresh", new RefreshTokenRequest(refreshToken), ct);
|
||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||
return null;
|
||
return (await ApiResponse.ReadAsync<AuthResponse>(response, ct)).Tokens;
|
||
}
|
||
}
|