auth
This commit is contained in:
parent
51e29884cd
commit
ac4c7c0656
8 changed files with 59 additions and 12 deletions
|
@ -1,5 +1,6 @@
|
||||||
<Router AppAssembly="@typeof(App).Assembly">
|
<Router AppAssembly="@typeof(App).Assembly">
|
||||||
<Found Context="routeData">
|
<Found Context="routeData">
|
||||||
|
<AuthenticateUser />
|
||||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
|
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
|
||||||
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
|
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
|
||||||
</Found>
|
</Found>
|
||||||
|
|
|
@ -40,7 +40,15 @@
|
||||||
public LoginModel Model { get; set; } = new();
|
public LoginModel Model { get; set; } = new();
|
||||||
public string ValidationMsg = string.Empty;
|
public string ValidationMsg = string.Empty;
|
||||||
|
|
||||||
protected override void OnInitialized() => Model ??= new();
|
protected async override Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
if (await usr.GetUser() != null)
|
||||||
|
{
|
||||||
|
nav.NavigateTo("/");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Model ??= new();
|
||||||
|
}
|
||||||
|
|
||||||
private async Task Submit()
|
private async Task Submit()
|
||||||
{
|
{
|
||||||
|
@ -56,7 +64,7 @@
|
||||||
this.StateHasChanged();
|
this.StateHasChanged();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
usr.User = user;
|
usr.SetUser(user);
|
||||||
this.StateHasChanged();
|
this.StateHasChanged();
|
||||||
nav.NavigateTo("/");
|
nav.NavigateTo("/");
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
var resp = await req.GetAsync("/api/auth/logout");
|
var resp = await req.GetAsync("/api/auth/logout");
|
||||||
if (resp.StatusCode == HttpStatusCode.NoContent)
|
if (resp.StatusCode == HttpStatusCode.NoContent)
|
||||||
{
|
{
|
||||||
usr.User = null!;
|
usr.SetUser(null!);
|
||||||
}
|
}
|
||||||
nav.NavigateTo("/login");
|
nav.NavigateTo("/login");
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,7 +52,15 @@
|
||||||
public LoginModel Model { get; set; } = new();
|
public LoginModel Model { get; set; } = new();
|
||||||
public string ValidationMsg = string.Empty;
|
public string ValidationMsg = string.Empty;
|
||||||
|
|
||||||
protected override void OnInitialized() => Model ??= new();
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
if (await usr.GetUser() != null)
|
||||||
|
{
|
||||||
|
nav.NavigateTo("/");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Model ??= new();
|
||||||
|
}
|
||||||
|
|
||||||
private async Task Submit()
|
private async Task Submit()
|
||||||
{
|
{
|
||||||
|
@ -67,7 +75,7 @@
|
||||||
this.StateHasChanged();
|
this.StateHasChanged();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
usr.User = user;
|
usr.SetUser(user);
|
||||||
this.StateHasChanged();
|
this.StateHasChanged();
|
||||||
nav.NavigateTo("/");
|
nav.NavigateTo("/");
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -27,8 +27,6 @@ internal class Program
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
await app.Services.GetRequiredService<UserManager>().InitAsync();
|
|
||||||
|
|
||||||
await builder.Build().RunAsync();
|
await builder.Build().RunAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ public class UserManager
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly IServiceProvider _serv;
|
private readonly IServiceProvider _serv;
|
||||||
|
private bool _init = false;
|
||||||
|
static SemaphoreSlim sem = new SemaphoreSlim(1, 1);
|
||||||
|
|
||||||
|
|
||||||
public async Task InitAsync()
|
public async Task InitAsync()
|
||||||
|
@ -24,11 +26,30 @@ public class UserManager
|
||||||
var resp = await scope.ServiceProvider.GetRequiredService<HttpClient>().GetAsync("/api/auth/userdata");
|
var resp = await scope.ServiceProvider.GetRequiredService<HttpClient>().GetAsync("/api/auth/userdata");
|
||||||
if (resp.StatusCode == HttpStatusCode.OK)
|
if (resp.StatusCode == HttpStatusCode.OK)
|
||||||
{
|
{
|
||||||
User = (await resp.Content.ReadFromJsonAsync<UserModel>())!;
|
this.User = (await resp.Content.ReadFromJsonAsync<UserModel>())!;
|
||||||
Console.WriteLine($"User: {User.Id}");
|
Console.WriteLine($"User: {User.Id}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserModel User { get; set; } = default!;
|
private UserModel User { get; set; } = default!;
|
||||||
|
public async Task<UserModel?> GetUser()
|
||||||
|
{
|
||||||
|
if (User == null)
|
||||||
|
{
|
||||||
|
await sem.WaitAsync();
|
||||||
|
if (!_init)
|
||||||
|
{
|
||||||
|
await InitAsync();
|
||||||
|
}
|
||||||
|
sem.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
return User;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetUser(UserModel u)
|
||||||
|
{
|
||||||
|
User = u;
|
||||||
|
}
|
||||||
public bool IsAuthorized => User != null;
|
public bool IsAuthorized => User != null;
|
||||||
}
|
}
|
||||||
|
|
10
src/Interlinked.Core/Shared/AuthenticateUser.razor
Normal file
10
src/Interlinked.Core/Shared/AuthenticateUser.razor
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
@inject UserManager usr
|
||||||
|
|
||||||
|
@code {
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
await Task.Yield();
|
||||||
|
await usr.InitAsync();
|
||||||
|
this.StateHasChanged();
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
<a href="/logout">Log out (@usr.User.Username)</a>
|
<a href="/logout">Log out (@u?.Username)</a>
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -26,9 +26,10 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@code {
|
@code {
|
||||||
|
private UserModel u;
|
||||||
protected override async Task OnInitializedAsync()
|
protected override async Task OnInitializedAsync()
|
||||||
{
|
{
|
||||||
Console.WriteLine($"meow: {usr.IsAuthorized}");
|
await Task.Yield();
|
||||||
Console.WriteLine($"Meow: {usr.User.Id}");
|
u = await usr.GetUser()!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue