Authentication implementation
This commit is contained in:
+127
-28
@@ -1,44 +1,143 @@
|
||||
@page "/login"
|
||||
@page "/login"
|
||||
@using System.ComponentModel.DataAnnotations
|
||||
@using WebApp.Authentication
|
||||
@inject NavigationManager Navigation
|
||||
@inject LoginRateLimitService RateLimitService
|
||||
@inject IHttpContextAccessor HttpContextAccessor
|
||||
|
||||
<h3>Login</h3>
|
||||
<div class="d-flex justify-center align-center" style="min-height: 100vh;">
|
||||
<MudPaper Elevation="3" Class="pa-8" MaxWidth="400px" Style="width: 100%;">
|
||||
<MudText Typo="Typo.h4" Align="Align.Center" GutterBottom="true">
|
||||
TSA Chapter Organizer
|
||||
</MudText>
|
||||
<MudText Typo="Typo.h6" Align="Align.Center" GutterBottom="true" Class="mb-6">
|
||||
Sign In
|
||||
</MudText>
|
||||
|
||||
<form action="Auth/CookieLogin" method="post" >
|
||||
<button type="submit" class="btn btn-primary">Login</button>
|
||||
</form>
|
||||
@if (!string.IsNullOrEmpty(_errorMessage))
|
||||
{
|
||||
<MudAlert Severity="Severity.Error" Class="mb-4">@_errorMessage</MudAlert>
|
||||
}
|
||||
|
||||
<MudText Typo="Typo.h4" GutterBottom="true">Sign In</MudText>
|
||||
<EditForm Model="@_loginModel" OnValidSubmit="HandleLogin">
|
||||
<DataAnnotationsValidator />
|
||||
|
||||
@* <MudTextField T="string" Value="@("staff@mudblazor.com")" Label="E-mail" Variant="Variant.Outlined" Class="my-6"></MudTextField> *@
|
||||
<MudTextField @bind-Value="_loginModel.Email"
|
||||
Label="Email"
|
||||
Variant="Variant.Outlined"
|
||||
InputType="InputType.Email"
|
||||
Required="true"
|
||||
Class="mb-4"
|
||||
Disabled="@_isSubmitting"
|
||||
For="@(() => _loginModel.Email)" />
|
||||
|
||||
<MudTextField @bind-Value="@Password" Label="Password" Variant="Variant.Outlined" InputType="@PasswordInput" Adornment="Adornment.End" AdornmentIcon="@PasswordInputIcon" OnAdornmentClick="TogglePasswordVisibility" />
|
||||
<MudTextField @bind-Value="_loginModel.Password"
|
||||
Label="Password"
|
||||
Variant="Variant.Outlined"
|
||||
InputType="@_passwordInput"
|
||||
Required="true"
|
||||
Class="mb-4"
|
||||
Disabled="@_isSubmitting"
|
||||
Adornment="Adornment.End"
|
||||
AdornmentIcon="@_passwordInputIcon"
|
||||
OnAdornmentClick="TogglePasswordVisibility"
|
||||
For="@(() => _loginModel.Password)" />
|
||||
|
||||
<div class="d-flex justify-space-between align-center">
|
||||
<MudCheckBox T="bool" Label="Remember me?" Color="Color.Primary" Class="ml-n1 my-3"></MudCheckBox>
|
||||
<MudLink Href="/pages/authentication/forgot-password">Forgot pwd?</MudLink>
|
||||
<div class="d-flex justify-space-between align-center mb-4">
|
||||
<MudCheckBox @bind-Value="_loginModel.RememberMe"
|
||||
Label="Remember me?"
|
||||
Color="Color.Primary"
|
||||
Disabled="@_isSubmitting" />
|
||||
</div>
|
||||
|
||||
<MudButton ButtonType="ButtonType.Submit"
|
||||
Variant="Variant.Filled"
|
||||
Color="Color.Primary"
|
||||
Size="Size.Large"
|
||||
FullWidth="true"
|
||||
Disabled="@_isSubmitting">
|
||||
@if (_isSubmitting)
|
||||
{
|
||||
<MudProgressCircular Size="Size.Small" Indeterminate="true" Class="mr-2" />
|
||||
<span>Signing in...</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Sign In</span>
|
||||
}
|
||||
</MudButton>
|
||||
</EditForm>
|
||||
</MudPaper>
|
||||
</div>
|
||||
|
||||
<MudButton Variant="Variant.Filled" Color="Color.Primary" Link="/personal/dashboard" Size="Size.Large" FullWidth="true">Sign In</MudButton>
|
||||
|
||||
@code {
|
||||
string Password { get; set; } = "BMWvBPJXZu";
|
||||
private LoginModel _loginModel = new();
|
||||
private string? _errorMessage;
|
||||
private bool _isSubmitting = false;
|
||||
private bool _passwordVisibility;
|
||||
private InputType _passwordInput = InputType.Password;
|
||||
private string _passwordInputIcon = Icons.Material.Filled.VisibilityOff;
|
||||
|
||||
bool PasswordVisibility;
|
||||
InputType PasswordInput = InputType.Password;
|
||||
string PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
|
||||
|
||||
void TogglePasswordVisibility()
|
||||
private class LoginModel
|
||||
{
|
||||
@if (PasswordVisibility)
|
||||
[Required(ErrorMessage = "Email is required")]
|
||||
[EmailAddress(ErrorMessage = "Invalid email format")]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
[Required(ErrorMessage = "Password is required")]
|
||||
[MinLength(8, ErrorMessage = "Password must be at least 8 characters")]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
public bool RememberMe { get; set; }
|
||||
}
|
||||
|
||||
private void TogglePasswordVisibility()
|
||||
{
|
||||
if (_isSubmitting) return;
|
||||
|
||||
_passwordVisibility = !_passwordVisibility;
|
||||
_passwordInputIcon = _passwordVisibility
|
||||
? Icons.Material.Filled.Visibility
|
||||
: Icons.Material.Filled.VisibilityOff;
|
||||
_passwordInput = _passwordVisibility
|
||||
? InputType.Text
|
||||
: InputType.Password;
|
||||
}
|
||||
|
||||
private async Task HandleLogin()
|
||||
{
|
||||
_isSubmitting = true;
|
||||
_errorMessage = null;
|
||||
|
||||
try
|
||||
{
|
||||
PasswordVisibility = false;
|
||||
PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
|
||||
PasswordInput = InputType.Password;
|
||||
// Get client IP address
|
||||
var ipAddress = HttpContextAccessor.HttpContext?.Connection.RemoteIpAddress?.ToString()
|
||||
?? "unknown";
|
||||
|
||||
// Check rate limiting
|
||||
if (RateLimitService.IsLockedOut(ipAddress))
|
||||
{
|
||||
var remaining = RateLimitService.GetRemainingLockoutTime(ipAddress);
|
||||
_errorMessage = $"Too many failed attempts. Please try again in {remaining?.Minutes ?? 15} minutes.";
|
||||
return;
|
||||
}
|
||||
|
||||
// Navigate to controller endpoint with credentials
|
||||
var uri = $"/Auth/CookieLogin?email={Uri.EscapeDataString(_loginModel.Email)}" +
|
||||
$"&password={Uri.EscapeDataString(_loginModel.Password)}" +
|
||||
$"&rememberMe={_loginModel.RememberMe}";
|
||||
|
||||
Navigation.NavigateTo(uri, forceLoad: true);
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
PasswordVisibility = true;
|
||||
PasswordInputIcon = Icons.Material.Filled.Visibility;
|
||||
PasswordInput = InputType.Text;
|
||||
_errorMessage = "An error occurred during login. Please try again.";
|
||||
Console.WriteLine($"Login error: {ex}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isSubmitting = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
@page "/import"
|
||||
@attribute [Authorize]
|
||||
@attribute [Authorize(Roles = AuthRoles.Administrator)]
|
||||
@using Core.Parsers
|
||||
@using Microsoft.EntityFrameworkCore
|
||||
@using WebApp.Authentication
|
||||
@inject AppDbContext Context
|
||||
|
||||
@rendermode InteractiveServer
|
||||
|
||||
Reference in New Issue
Block a user