diff --git a/Pages/Login.razor b/Pages/Login.razor index 4a26ec3..7a026c8 100644 --- a/Pages/Login.razor +++ b/Pages/Login.razor @@ -23,35 +23,76 @@ @inject ILocalStorageService localStorage @inject IDialogService DialogService @inject SettingsService savedSettings +@inject IJSRuntime JSRuntime Login -

Login

-

- TBD -

+ +

Login

-@LoginCode +

+ You'll need to sign in with the Blazing API before using this site. +

-Refresh Code -Finish Sign-in -Change API Server +

+ + This device + Another device + +

+ +

+ Next +

+ + @if (HasStarted) { + @if (IsExternal) { +

+ Visit @savedSettings.Settings.RelativeUrl("/auth/link") + on another device to sign in, and enter the code below: +

+

@LoginCode

+ } else { +

+ Authorize the app in the popup window to login. +

+ } +

+ + Done + @if (IsExternal) { + Refresh Code + } else { + Try Again + } + Cancel + +

+ } +
+

+ API Settings +

+
+ + + + -@if (IsReady) { -

no way, it works

- Go to Home -} @code { public string LoginCode { get; set; } = ""; public string LoginKey { get; set; } = ""; - public bool IsReady { get; set; } = false; + public bool HasStarted { get; set; } = false; + public bool IsExternal { get; set; } = false; + public bool IsBusy { get; set; } = false; protected override async Task OnInitializedAsync() { - await RefreshCode(); + await RefreshCodeAsync(); } - public async Task RefreshCode() { + async Task RefreshCodeAsync() { var req = new PrepareAuthFlowRequest("Blazing Console", "bedcrab.dev", "A web panel to edit your Blazing Games computers", new List { Permission.READ_COMPUTERS, Permission.WRITE_COMPUTERS, @@ -72,7 +113,25 @@ LoginKey = res.Key; } - public async Task FinishSignIn() { + async Task BeginFlowAsync() { + IsBusy = true; + HasStarted = false; + + await RefreshCodeAsync(); + if (!IsExternal) { + await JSRuntime.InvokeVoidAsync("open", savedSettings.Settings.RelativeUrl("/auth/link?code=" + LoginCode), "_blank"); + } + + IsBusy = false; + HasStarted = true; + } + + void AbortFlowAsync() { + HasStarted = false; + } + + async Task FinishLoginAsync() { + IsBusy = true; var req = new RedeemAuthFlowRequest(LoginKey); var res = await api.SendHttpRequest("/auth/redeem", HttpMethod.Post, req); if (res == null) { @@ -82,15 +141,14 @@ bool works = await api.TestAuthorization(); if (works) { await localStorage.SetItemAsync("jwt", res.Token); - IsReady = true; + Navigation.NavigateTo("/"); + } else { + IsBusy = false; + await DialogService.ShowErrorAsync("Failed to sign in. Make sure you've approved the sign-in request."); } } - public void GotoHome() { - Navigation.NavigateTo("/"); - } - - public async Task ChangeServerAsync() { + async Task ChangeServerAsync() { string currentServer = savedSettings.Settings.Clone().ApiURL; var changeDialog = await DialogService.ShowDialogAsync(currentServer, new DialogParameters()); var result = await changeDialog.Result;