Added auth flow
This commit is contained in:
57
OnProfNext.Client/Pages/Login.razor
Normal file
57
OnProfNext.Client/Pages/Login.razor
Normal file
@@ -0,0 +1,57 @@
|
||||
@page "/login"
|
||||
@using OnProfNext.Shared.Models
|
||||
@inject HttpClient Http
|
||||
@inject NavigationManager Nav
|
||||
@inject IJSRuntime JS
|
||||
|
||||
<h3>Anmeldung</h3>
|
||||
|
||||
@if (error is not null)
|
||||
{
|
||||
<div class="alert alert-danger">@error</div>
|
||||
}
|
||||
|
||||
<EditForm Model="login" OnValidSubmit="HandleLogin">
|
||||
<DataAnnotationsValidator />
|
||||
<ValidationSummary />
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Benutzername</label>
|
||||
<InputText class="form-control" @bind-Value="login.Username" />
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label>Passwort</label>
|
||||
<InputText type="password" class="form-control" @bind-Value="login.Password" />
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary w-100">Login</button>
|
||||
</EditForm>
|
||||
|
||||
|
||||
@code {
|
||||
private LoginRequest login = new();
|
||||
private string? error;
|
||||
|
||||
private async Task HandleLogin()
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = await Http.PostAsJsonAsync("api/auth/login", login);
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
error = "Anmeldung fehlgeschlagen.";
|
||||
return;
|
||||
}
|
||||
|
||||
var result = await response.Content.ReadFromJsonAsync<LoginResponse>();
|
||||
await JS.InvokeVoidAsync("localStorage.setItem", "authToken", result!.Token);
|
||||
|
||||
Nav.NavigateTo("/users");
|
||||
}
|
||||
catch
|
||||
{
|
||||
error = "Server nicht erreichbar.";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
@using OnProfNext.Client.Services
|
||||
@using OnProfNext.Shared.Models
|
||||
@inject UserApiService UserService
|
||||
@inject AuthService AuthService
|
||||
@inject NavigationManager Nav
|
||||
|
||||
<h3 class="mb-3">Benutzerübersicht</h3>
|
||||
|
||||
@@ -147,6 +149,15 @@ else
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
var isLoggedIn = await AuthService.IsLoggedInAsync();
|
||||
if (!isLoggedIn)
|
||||
{
|
||||
Nav.NavigateTo("/login", forceLoad: true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
var result = await UserService.GetUsersAsync();
|
||||
|
||||
if (!result.Success)
|
||||
|
||||
Reference in New Issue
Block a user