Added authenication/authorization. Refactored api startup.
This commit is contained in:
86
JSMR.UI.Blazor/Pages/Login.razor
Normal file
86
JSMR.UI.Blazor/Pages/Login.razor
Normal file
@@ -0,0 +1,86 @@
|
||||
@page "/login"
|
||||
|
||||
@using JSMR.UI.Blazor.Services
|
||||
|
||||
@inject SessionState Session
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<h3>Login</h3>
|
||||
|
||||
@if (Session.IsAuthenticated)
|
||||
{
|
||||
<p>You're already logged in as <b>@Session.Me?.name</b>.</p>
|
||||
<button @onclick="Logout">Logout</button>
|
||||
}
|
||||
else
|
||||
{
|
||||
<div style="max-width: 360px;">
|
||||
<div>
|
||||
<label>Username</label><br />
|
||||
<input @bind="username" />
|
||||
</div>
|
||||
<div style="margin-top: 8px;">
|
||||
<label>Password</label><br />
|
||||
<input type="password" @bind="password" />
|
||||
</div>
|
||||
|
||||
<div style="margin-top: 12px;">
|
||||
<button @onclick="LoginAsync" disabled="@busy">Login</button>
|
||||
</div>
|
||||
|
||||
@if (!string.IsNullOrWhiteSpace(error))
|
||||
{
|
||||
<p style="color: crimson; margin-top: 8px;">@error</p>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
@code {
|
||||
private string username = "";
|
||||
private string password = "";
|
||||
private bool busy;
|
||||
private string? error;
|
||||
|
||||
private async Task LoginAsync()
|
||||
{
|
||||
busy = true;
|
||||
error = null;
|
||||
try
|
||||
{
|
||||
var ok = await Session.LoginAsync(username, password);
|
||||
if (!ok)
|
||||
{
|
||||
error = "Invalid username or password.";
|
||||
return;
|
||||
}
|
||||
|
||||
Nav.NavigateTo("/");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Logout()
|
||||
{
|
||||
busy = true;
|
||||
error = null;
|
||||
try
|
||||
{
|
||||
await Session.LogoutAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
error = ex.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user