78 lines
1.7 KiB
Plaintext
78 lines
1.7 KiB
Plaintext
@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);
|
|
}
|
|
} |