Files
Heimdall/src/Heimdall.Web/Components/Pages/Register.razor
T

121 lines
4.2 KiB
Plaintext
Raw Normal View History

@page "/register"
@layout MudBlazorLayout
@inject HeimdallApiClient Api
@inject AuthState Auth
@inject NavigationManager Navigation
@inject ISnackbar Snackbar
@inject HeimdallAuthStateProvider AuthStateProvider
@inject IJSRuntime JsRuntime
<div class="auth-page">
<div class="auth-card">
<div class="auth-header">
<div class="auth-icon">
<MudIcon Icon="@Icons.Material.Filled.Shield" Size="Size.Large" />
</div>
<h1>Créer un compte</h1>
<p>Rejoignez la plateforme Heimdall</p>
</div>
<div class="auth-form">
<form @onsubmit="RegisterAsync" method="post">
<MudTextField @bind-Value="_displayName"
Label="Nom d'affichage"
Variant="Variant.Outlined"
Class="mb-4"
Required="true"
Immediate="true"
Autocomplete="name" />
<MudTextField @bind-Value="_email"
Label="Email"
Variant="Variant.Outlined"
Class="mb-4"
Required="true"
Immediate="true"
Autocomplete="email" />
<MudTextField @bind-Value="_password"
Label="Mot de passe"
Variant="Variant.Outlined"
InputType="InputType.Password"
Class="mb-4"
Required="true"
Immediate="true"
HelperText="Minimum 12 caractères"
Autocomplete="new-password" />
<div class="auth-error">
@if (!string.IsNullOrEmpty(_errorMessage))
{
<MudAlert Severity="Severity.Error">@_errorMessage</MudAlert>
}
</div>
<MudButton Variant="Variant.Filled"
Color="Color.Primary"
FullWidth="true"
ButtonType="ButtonType.Submit"
Disabled="@_loading"
Class="mud-button-submit">
@if (_loading)
{
<MudProgressCircular Size="Size.Small" Class="mr-2" Color="Color.Inherit" />
}
@(_loading ? "Création en cours…" : "S'inscrire")
</MudButton>
</form>
</div>
<div class="auth-footer">
<p>
Déjà un compte ?
<a href="/login">Se connecter</a>
</p>
</div>
</div>
</div>
@code {
private string _email = "";
private string _password = "";
private string _displayName = "";
private bool _loading;
private string _errorMessage = "";
private async Task RegisterAsync()
{
_loading = true;
_errorMessage = "";
try
{
var result = await Api.RegisterAsync(_email, _password, _displayName);
if (result is null)
{
_errorMessage = "Erreur lors de l'inscription";
return;
}
// Update Blazor-level auth state
await AuthStateProvider.NotifyLoginAsync(result);
// Create middleware-level auth cookie
if (result.User is not null)
{
await JsRuntime.InvokeVoidAsync("heimdallAuth.signIn", new
{
id = result.User.Id.ToString(),
email = result.User.Email,
displayName = result.User.DisplayName,
roles = result.User.Roles
});
}
Snackbar.Add("Compte créé avec succès", Severity.Success);
Navigation.NavigateTo("/dashboard");
}
catch (Exception ex)
{
_errorMessage = $"Erreur : {ex.Message}";
}
finally { _loading = false; }
}
}