From 03313562d21d5db9bf6a14ebbeab80e06c883d3a Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 24 Jan 2024 02:31:56 +0100 Subject: MRU->RMU, fixes, cleanup --- MatrixUtils.Web/Pages/InvalidSession.razor | 100 +++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 MatrixUtils.Web/Pages/InvalidSession.razor (limited to 'MatrixUtils.Web/Pages/InvalidSession.razor') diff --git a/MatrixUtils.Web/Pages/InvalidSession.razor b/MatrixUtils.Web/Pages/InvalidSession.razor new file mode 100644 index 0000000..e1a72ea --- /dev/null +++ b/MatrixUtils.Web/Pages/InvalidSession.razor @@ -0,0 +1,100 @@ +@page "/InvalidSession" +@using LibMatrix + +Invalid session + +

Rory&::MatrixUtils - Invalid session encountered

+

A session was encountered that is no longer valid. This can happen if you have logged out of the account on another device, or if the access token has expired.

+ +@if (_login is not null) { +

It appears that the affected user is @_login.UserId (@_login.DeviceId) on @_login.Homeserver!

+ Refresh token + Remove + + @if (_showRefreshDialog) { + +
+ Log in + @if (_loginException is not null) { +
@_loginException.RawContent
+ } +
+ } +} +else { + Something has gone wrong and the login was not passed along! +} + +@code +{ + [Parameter] + [SupplyParameterFromQuery(Name = "ctx")] + public string Context { get; set; } + + private UserAuth? _login { get; set; } + + private bool _showRefreshDialog { get; set; } + + private string _password { get; set; } = ""; + + private MatrixException? _loginException { get; set; } + + protected override async Task OnInitializedAsync() { + var tokens = await RMUStorage.GetAllTokens(); + if (tokens is null || tokens.Count == 0) { + NavigationManager.NavigateTo("/Login"); + return; + } + + _login = tokens.FirstOrDefault(x => x.AccessToken == Context); + + if (_login is null) { + Console.WriteLine($"Could not find {_login} in stored tokens!"); + } + + await base.OnInitializedAsync(); + } + + private async Task RemoveUser() { + await RMUStorage.RemoveToken(_login!); + if ((await RMUStorage.GetCurrentToken())!.AccessToken == _login!.AccessToken) + await RMUStorage.SetCurrentToken((await RMUStorage.GetAllTokens())?.FirstOrDefault()); + await OnInitializedAsync(); + } + + private async Task OpenRefreshDialog() { + _showRefreshDialog = true; + StateHasChanged(); + await Task.CompletedTask; + } + + private async Task SwitchSession(UserAuth auth) { + Console.WriteLine($"Switching to {auth.Homeserver} {auth.AccessToken} {auth.UserId}"); + await RMUStorage.SetCurrentToken(auth); + await OnInitializedAsync(); + } + + private async Task TryLogin() { + if(_login is null) throw new NullReferenceException("Login is null!"); + try { + var result = new UserAuth(await hsProvider.Login(_login.Homeserver, _login.UserId, _password)); + if (result is null) { + Console.WriteLine($"Failed to login to {_login.Homeserver} as {_login.UserId}!"); + return; + } + Console.WriteLine($"Obtained access token for {result.UserId}!"); + + await RemoveUser(); + await RMUStorage.AddToken(result); + if (result.UserId == (await RMUStorage.GetCurrentToken())?.UserId) + await RMUStorage.SetCurrentToken(result); + NavigationManager.NavigateTo("/"); + } + catch (MatrixException e) { + Console.WriteLine($"Failed to login to {_login.Homeserver} as {_login.UserId}!"); + Console.WriteLine(e); + _loginException = e; + StateHasChanged(); + } + } +} -- cgit 1.4.1