Started testing oauth2 handler from mooncore

This commit is contained in:
Masu Baumgartner
2024-11-05 22:46:26 +01:00
parent 69e5e1c75b
commit 288b0c8d97
11 changed files with 337 additions and 45 deletions

View File

@@ -0,0 +1,78 @@
@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);
}
}