@page "/account/register"
@layout AuthLayout
@attribute [AllowAnonymous]
@using Microsoft.AspNetCore.Components.Forms
@using NexusReader.UI.Shared.Services
@using NexusReader.UI.Shared.Components.Atoms
@inject IIdentityService IdentityService
@inject NavigationManager NavigationManager
@inject IJSRuntime JS
@inject FeatureSettings FeatureSettings
@if (!string.IsNullOrEmpty(_errorMessage))
{
@_errorMessage
}
@code {
private RegisterModel _registerModel = new();
private string? _errorMessage;
private bool _isSubmitting;
protected override void OnInitialized()
{
var allowRegistration = FeatureSettings.AllowRegistration;
if (!allowRegistration)
{
NavigationManager.NavigateTo("/account/login?error=RegistrationDisabled", replace: true);
}
}
private async Task HandleRegister()
{
_isSubmitting = true;
_errorMessage = null;
try
{
var regResult = await IdentityService.RegisterAsync(_registerModel.Email, _registerModel.Password);
if (regResult.IsSuccess)
{
var loginResult = await IdentityService.LoginAsync(_registerModel.Email, _registerModel.Password);
if (loginResult.IsSuccess)
{
// Trigger hidden form submission via robust JS helper to perform cookie-based sign-in
await JS.InvokeVoidAsync("nexusAuth.submitLoginForm", "nexusLoginForm", _registerModel.Email, _registerModel.Password, false);
}
else
{
NavigationManager.NavigateTo("/account/login");
}
}
else
{
_errorMessage = regResult.Errors.FirstOrDefault()?.Message ?? "Rejestracja nie powiodła się.";
}
}
catch (Exception)
{
_errorMessage = "Wystąpił błąd podczas rejestracji.";
}
finally
{
_isSubmitting = false;
}
}
public class RegisterModel
{
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "E-mail jest wymagany")]
[System.ComponentModel.DataAnnotations.EmailAddress(ErrorMessage = "Nieprawidłowy e-mail")]
public string Email { get; set; } = string.Empty;
[System.ComponentModel.DataAnnotations.Required(ErrorMessage = "Hasło jest wymagane")]
[System.ComponentModel.DataAnnotations.MinLength(8, ErrorMessage = "Minimum 8 znaków")]
public string Password { get; set; } = string.Empty;
[System.ComponentModel.DataAnnotations.Compare(nameof(Password), ErrorMessage = "Hasła nie są identyczne")]
public string ConfirmPassword { get; set; } = string.Empty;
}
}