183 lines
6.0 KiB
Plaintext
183 lines
6.0 KiB
Plaintext
@page "/account/register"
|
|
@layout AuthenticationLayout
|
|
|
|
@using Indotalent.Shared.Consts
|
|
@using Microsoft.JSInterop
|
|
@inject IJSRuntime JSRuntime
|
|
@inject ISnackbar Snackbar
|
|
@inject NavigationManager Navigation
|
|
@inject IConfiguration Configuration
|
|
|
|
<PageTitle>Sign Up - @GlobalConsts.AppInitial</PageTitle>
|
|
|
|
<div class="page-header">
|
|
<h2>Sign Up</h2>
|
|
<p>Please register your account</p>
|
|
</div>
|
|
|
|
<MudForm @ref="form" @bind-IsValid="@success" Validation="@(new Func<EditContext, Task<bool>>(ValidateForm))">
|
|
|
|
<label class="form-label">Full Name</label>
|
|
<MudTextField T="string"
|
|
@bind-Value="model.FullName"
|
|
Variant="Variant.Outlined"
|
|
Placeholder="Enter your full name"
|
|
Margin="Margin.Dense"
|
|
Adornment="Adornment.Start"
|
|
AdornmentIcon="@Icons.Material.Filled.Person"
|
|
Required="true"
|
|
For="@(() => model.FullName)"
|
|
Class="mb-4" />
|
|
|
|
<label class="form-label">Email Address</label>
|
|
<MudTextField T="string"
|
|
@bind-Value="model.Email"
|
|
Variant="Variant.Outlined"
|
|
Placeholder="Enter your email"
|
|
Margin="Margin.Dense"
|
|
Adornment="Adornment.Start"
|
|
AdornmentIcon="@Icons.Material.Filled.Email"
|
|
Required="true"
|
|
For="@(() => model.Email)"
|
|
Class="mb-4" />
|
|
|
|
<label class="form-label">Password</label>
|
|
<MudTextField T="string"
|
|
@bind-Value="model.Password"
|
|
Variant="Variant.Outlined"
|
|
Placeholder="Create a password"
|
|
Margin="Margin.Dense"
|
|
InputType="@(showPassword ? InputType.Text : InputType.Password)"
|
|
Adornment="Adornment.Start"
|
|
AdornmentIcon="@Icons.Material.Filled.Lock"
|
|
AdornmentEndIcon="@(showPassword? Icons.Material.Filled.Visibility : Icons.Material.Filled.VisibilityOff)"
|
|
OnAdornmentEndClick="() => showPassword = !showPassword"
|
|
Required="true"
|
|
For="@(() => model.Password)"
|
|
Class="mb-6" />
|
|
|
|
<MudButton Variant="Variant.Filled"
|
|
Color="Color.Primary"
|
|
Size="Size.Large"
|
|
FullWidth="true"
|
|
Disabled="!success || isProcessing"
|
|
OnClick="HandleRegister"
|
|
Style="text-transform:none; border-radius: 8px; height: 48px; font-weight: 600;">
|
|
@if (isProcessing)
|
|
{
|
|
<MudProgressCircular Class="ms-n1" Size="Size.Small" Indeterminate="true" Color="Color.Inherit" />
|
|
<span class="ms-2">Creating account...</span>
|
|
}
|
|
else
|
|
{
|
|
<span>Create Account</span>
|
|
}
|
|
</MudButton>
|
|
</MudForm>
|
|
|
|
<div style="text-align: center; margin-top: 1.5rem;">
|
|
<span style="font-size: 0.875rem; color: #64748b;">
|
|
Already have an account?
|
|
<a class="auth-link" href="/account/login">Sign In</a>
|
|
</span>
|
|
</div>
|
|
|
|
@code {
|
|
private MudForm form = default!;
|
|
private bool success;
|
|
private bool isProcessing;
|
|
private bool showPassword;
|
|
|
|
private RegisterModel model = new();
|
|
|
|
private class RegisterModel
|
|
{
|
|
public string FullName { get; set; } = "";
|
|
public string Email { get; set; } = "";
|
|
public string Password { get; set; } = "";
|
|
}
|
|
|
|
private async Task<bool> ValidateForm(EditContext context)
|
|
{
|
|
return await Task.FromResult(true);
|
|
}
|
|
|
|
private async Task HandleRegister()
|
|
{
|
|
await form.Validate();
|
|
|
|
if (!success) return;
|
|
|
|
isProcessing = true;
|
|
StateHasChanged();
|
|
|
|
var result = await JSRuntime.InvokeAsync<ApiJSRuntimeResponse>("apiAccountRegister", model.FullName, model.Email, model.Password);
|
|
|
|
if (result.Status == 200)
|
|
{
|
|
var requireConfirmation = Configuration.GetValue<bool>("IdentitySettings:SignIn:RequireConfirmedAccount");
|
|
|
|
if (requireConfirmation)
|
|
{
|
|
Snackbar.Add("Registration successful! Please check your email to confirm your account.", Severity.Info);
|
|
}
|
|
else
|
|
{
|
|
Snackbar.Add("Registration successful! You can now sign in.", Severity.Success);
|
|
}
|
|
|
|
await Task.Delay(500);
|
|
Navigation.NavigateTo("/account/login");
|
|
}
|
|
else
|
|
{
|
|
Snackbar.Add(result.Title ?? "Registration failed", Severity.Error);
|
|
}
|
|
|
|
isProcessing = false;
|
|
StateHasChanged();
|
|
}
|
|
|
|
private class ApiJSRuntimeResponse
|
|
{
|
|
public int? Status { get; set; }
|
|
public string? Title { get; set; }
|
|
}
|
|
}
|
|
|
|
<script>
|
|
window.apiAccountRegister = async (fullName, email, password) => {
|
|
try {
|
|
const response = await fetch('/api/account/signup', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify({ fullName, email, password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok) {
|
|
return { status: 200, title: 'Success account registration' };
|
|
} else {
|
|
let errorMessage = data.title || data.message || 'Registration failed';
|
|
|
|
if (data.errors) {
|
|
if (Array.isArray(data.errors) && data.errors.length > 0) {
|
|
errorMessage = data.errors[0];
|
|
} else if (typeof data.errors === 'object') {
|
|
const firstKey = Object.keys(data.errors)[0];
|
|
if (firstKey) {
|
|
errorMessage = data.errors[firstKey][0];
|
|
}
|
|
}
|
|
}
|
|
|
|
return { status: response.status, title: errorMessage };
|
|
}
|
|
} catch (error) {
|
|
return { status: 500, title: 'Server / Network error' };
|
|
}
|
|
};
|
|
</script> |