about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Classes
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-17 00:21:24 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-17 00:21:24 +0200
commit1beca653b772cf10586c417b2c25df03a67df8a2 (patch)
treed539b5f329cb62f253e1bc8142c3a313719657b0 /MatrixRoomUtils.Web/Classes
parentChanges (diff)
downloadMatrixUtils-1beca653b772cf10586c417b2c25df03a67df8a2.tar.xz
Handle external logouts
Diffstat (limited to 'MatrixRoomUtils.Web/Classes')
-rw-r--r--MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs40
1 files changed, 31 insertions, 9 deletions
diff --git a/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
index 41f604d..d4b256b 100644
--- a/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
+++ b/MatrixRoomUtils.Web/Classes/MRUStorageWrapper.cs
@@ -21,8 +21,10 @@ public class MRUStorageWrapper {
     }
 
     public async Task<List<LoginResponse>?> GetAllTokens() {
-        return await _storageService.DataStorageProvider.LoadObjectAsync<List<LoginResponse>>("mru.tokens") ?? new List<LoginResponse>();
+        return await _storageService.DataStorageProvider.LoadObjectAsync<List<LoginResponse>>("mru.tokens") ??
+               new List<LoginResponse>();
     }
+
     public async Task<LoginResponse?> GetCurrentToken() {
         var currentToken = await _storageService.DataStorageProvider.LoadObjectAsync<LoginResponse>("token");
         var allTokens = await GetAllTokens();
@@ -30,12 +32,15 @@ public class MRUStorageWrapper {
             await SetCurrentToken(null);
             return null;
         }
+
         if (currentToken is null) {
             await SetCurrentToken(currentToken = allTokens[0]);
         }
-        if(!allTokens.Any(x=>x.AccessToken == currentToken.AccessToken)) {
+
+        if (!allTokens.Any(x => x.AccessToken == currentToken.AccessToken)) {
             await SetCurrentToken(currentToken = allTokens[0]);
         }
+
         return currentToken;
     }
 
@@ -49,22 +54,39 @@ public class MRUStorageWrapper {
         await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens);
     }
 
-    public async Task<AuthenticatedHomeServer?> GetCurrentSession() {
+    private async Task<AuthenticatedHomeServer?> GetCurrentSession() {
         var token = await GetCurrentToken();
         if (token == null) {
             return null;
         }
-        
-        return await _homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken); 
+
+        return await _homeserverProviderService.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken);
     }
 
     public async Task<AuthenticatedHomeServer?> GetCurrentSessionOrNavigate() {
-        var session = await GetCurrentSession();
-        if (session == null) {
+        AuthenticatedHomeServer? session = null;
+
+        try {
+            //catch if the token is invalid
+            session = await GetCurrentSession();
+        }
+        catch (MatrixException e) {
+            if (e.ErrorCode == "M_UNKNOWN_TOKEN") {
+                var token = await GetCurrentToken();
+                _navigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken);
+                return null;
+            }
+
+            throw;
+        }
+
+        if (session is null) {
             _navigationManager.NavigateTo("/Login");
         }
+
         return session;
     }
+
     public class Settings {
         public DeveloperSettings DeveloperSettings { get; set; } = new();
     }
@@ -81,11 +103,11 @@ public class MRUStorageWrapper {
             return;
         }
 
-        tokens.RemoveAll(x=>x.AccessToken == auth.AccessToken);
+        tokens.RemoveAll(x => x.AccessToken == auth.AccessToken);
         await _storageService.DataStorageProvider.SaveObjectAsync("mru.tokens", tokens);
     }
 
     public async Task SetCurrentToken(LoginResponse? auth) {
         _storageService.DataStorageProvider.SaveObjectAsync("token", auth);
     }
-}
\ No newline at end of file
+}