85 lines
2.2 KiB
Text
85 lines
2.2 KiB
Text
@page "/login"
|
|
@using System.ComponentModel.DataAnnotations;
|
|
@using System.Net
|
|
@inject HttpClient req
|
|
@inject UserManager usr
|
|
@inject NavigationManager nav
|
|
|
|
<PageTitle>Login</PageTitle>
|
|
|
|
<h1>Log in</h1>
|
|
|
|
<EditForm class="form" Model="@Model" OnValidSubmit="@Submit">
|
|
<DataAnnotationsValidator />
|
|
<ValidationSummary />
|
|
<div class="form-group">
|
|
<label for="email">E-mail</label>
|
|
<InputText @bind-Value="Model!.Email" id="email" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="pwd">Password</label>
|
|
<InputText type="password" @bind-Value="Model!.Password" id="pwd" />
|
|
</div>
|
|
<button type="submit">Log in</button>
|
|
</EditForm>
|
|
|
|
@if (!string.IsNullOrWhiteSpace(ValidationMsg))
|
|
{
|
|
<p class="text-danger">@ValidationMsg</p>
|
|
}
|
|
|
|
@code {
|
|
public record LoginModel
|
|
{
|
|
[Required]
|
|
[EmailAddress]
|
|
public string Email { get; set; } = default!;
|
|
[Required]
|
|
public string Password { get; set; } = default!;
|
|
};
|
|
public LoginModel Model { get; set; } = new();
|
|
public string ValidationMsg = string.Empty;
|
|
|
|
protected async override Task OnInitializedAsync()
|
|
{
|
|
if (await usr.GetUser() != null)
|
|
{
|
|
nav.NavigateTo("/");
|
|
return;
|
|
}
|
|
Model ??= new();
|
|
}
|
|
|
|
private async Task Submit()
|
|
{
|
|
var resp = await req.PostAsJsonAsync("/api/auth/login", Model);
|
|
if (resp.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
Model = new();
|
|
ValidationMsg = "Successful login <3";
|
|
var user = await resp.Content.ReadFromJsonAsync<UserModel>();
|
|
if (user == null)
|
|
{
|
|
ValidationMsg = "Unknown authentication error";
|
|
this.StateHasChanged();
|
|
return;
|
|
}
|
|
usr.SetUser(user);
|
|
this.StateHasChanged();
|
|
nav.NavigateTo("/", true);
|
|
return;
|
|
}
|
|
|
|
if (resp.StatusCode == HttpStatusCode.Unauthorized)
|
|
{
|
|
Model = new();
|
|
ValidationMsg = "Invalid username or password.";
|
|
this.StateHasChanged();
|
|
return;
|
|
}
|
|
|
|
Model = new();
|
|
ValidationMsg = "Authorization error...";
|
|
this.StateHasChanged();
|
|
}
|
|
}
|