Adjusting to use new extension methods to configure and handle token auth and oauth2

This commit is contained in:
Masu-Baumgartner
2024-11-06 16:46:24 +01:00
parent 288b0c8d97
commit f2d653563c
8 changed files with 63 additions and 330 deletions

View File

@@ -1,78 +0,0 @@
@using Microsoft.AspNetCore.WebUtilities
@using MoonCore.Blazor.Services
@using MoonCore.Exceptions
@using MoonCore.Helpers
@inherits ErrorBoundaryBase
@inject NavigationManager Navigation
@inject OAuth2FrontendService OAuth2FrontendService
@if (IsCompleting)
{
}
else
{
@ChildContent
}
@code
{
private bool IsCompleting = false;
private string Code;
protected override void OnInitialized()
{
var uri = new Uri(Navigation.Uri);
if (!QueryHelpers.ParseQuery(uri.Query).TryGetValue("code", out var codeSv))
return;
try
{
if (codeSv.Count == 0 || string.IsNullOrEmpty(codeSv.First()))
return;
var code = codeSv.First();
Code = code!;
IsCompleting = true;
}
catch (Exception e)
{
Console.WriteLine("FUUCK");
Console.WriteLine(e);
throw;
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
return;
if(!IsCompleting)
return;
if (!await OAuth2FrontendService.Complete(Code))
await RedirectToAuth();
IsCompleting = false;
await InvokeAsync(StateHasChanged);
}
protected override async Task OnErrorAsync(Exception exception)
{
if (exception is not HttpApiException httpApiException || httpApiException.Status != 401)
throw exception;
// If we reach this point, we got a 401 unauthenticated, so we need to log in
await RedirectToAuth();
}
private async Task RedirectToAuth()
{
var url = await OAuth2FrontendService.Start();
Navigation.NavigateTo(url, true);
}
}