Skip to content
Snippets Groups Projects
Commit 03a564dc authored by Midhun Suresh's avatar Midhun Suresh
Browse files

Implement loading existing session

parent 5fdef677
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment