From 03a564dcf4c26aa37f64319b55e3fdd23ad1c2d9 Mon Sep 17 00:00:00 2001
From: Midhun Suresh <midhunr@element.io>
Date: Fri, 31 Dec 2021 12:58:11 +0530
Subject: [PATCH] Implement loading existing session

---
 src/Hydrogen.ts | 14 ++++++++++++++
 src/main.ts     | 25 +++++++++++++++++--------
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/src/Hydrogen.ts b/src/Hydrogen.ts
index 697fcfe..d597955 100644
--- a/src/Hydrogen.ts
+++ b/src/Hydrogen.ts
@@ -60,6 +60,20 @@ export class Hydrogen {
         this._container.appendChild(view.mount());
     }
 
+    /**
+     * Try to start Hydrogen based on an existing hydrogen session.
+     * If multiple sessions exist, this method chooses the most recent one.
+     */
+    async attemptStartWithExistingSession(): Promise<boolean> {
+        const sessionIds = await this._platform.sessionInfoStorage.getAll();
+        const { id } = sessionIds.pop();
+        if (id) {
+            await this._client.startWithExistingSession(id);
+            return true;
+        }
+        return false;
+    }
+
     private async _joinRoom(roomId: string): Promise<any> {
         await this._session.joinRoom(roomId);
         // even though we've joined the room, we need to wait till the next sync for the actual room
diff --git a/src/main.ts b/src/main.ts
index 08b8d87..35cc726 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -20,14 +20,23 @@ async function main() {
     const root = document.querySelector("#chatterbox") as HTMLDivElement;
     const { homeserver, auto_join_room } = await fetchConfig();
     const hydrogen = new Hydrogen(homeserver, root);
-    const username = generateRandomString(7);
-    const password = generateRandomString(10);
-    console.log(`Attempting to register with username = ${username} and password = ${password}`);
-    await hydrogen.register(username, password, "Chatterbox");
-    console.log("Registration done"); 
-    console.log("Attempting to login with same credentials");
-    await hydrogen.login(username, password);
-    console.log("Login successful");
+    console.log("Checking if session already exists");
+    const sessionAlreadyExists = await hydrogen.attemptStartWithExistingSession();
+    if (sessionAlreadyExists) {
+        console.log("Starting Hydrogen with existing session");
+    }
+    else {
+        console.log("Session does not exist!");
+        const username = generateRandomString(7);
+        const password = generateRandomString(10);
+        console.log(`Attempting to register with username = ${username} and password = ${password}`);
+        await hydrogen.register(username, password, "Chatterbox");
+        console.log("Registration done"); 
+        console.log("Attempting to login with same credentials");
+        await hydrogen.login(username, password);
+        console.log("Login successful");
+    }
+
     console.log("Attempting to mount Timeline");
     await hydrogen.showRoom(auto_join_room);
     console.log("Mounted Timeline");
-- 
GitLab