From cca08d318095f00424761e1cf997cbed0efe4189 Mon Sep 17 00:00:00 2001 From: guru-web3 <105355858+guru-web3@users.noreply.github.com> Date: Tue, 4 Jul 2023 18:00:39 +0530 Subject: [PATCH] fix: session logout --- src/auth/OpenLoginHandler.ts | 18 ++++++++++++++++++ src/modules/auth.ts | 10 +++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/auth/OpenLoginHandler.ts b/src/auth/OpenLoginHandler.ts index a6e83586..9ad6e5d8 100644 --- a/src/auth/OpenLoginHandler.ts +++ b/src/auth/OpenLoginHandler.ts @@ -1,13 +1,17 @@ import { PopupWithBcHandler, randomId } from "@toruslabs/base-controllers"; import { JRPCEngine, SafeEventEmitter } from "@toruslabs/openlogin-jrpc"; import { LOGIN_PROVIDER_TYPE, safebtoa } from "@toruslabs/openlogin-utils"; +import { Mutex } from "async-mutex"; import log from "loglevel"; import type { OpenLoginPopupResponse } from "@/utils/enums"; import config from "../config"; +import OpenLoginFactory from "./OpenLogin"; class OpenLoginHandler { + private static mutex = new Mutex(); + nonce = randomId(); windowId?: string; @@ -47,6 +51,20 @@ class OpenLoginHandler { ); } + static async getInstance(reinitialize = false) { + const releaseLock = await this.mutex.acquire(); + try { + const openLoginInstance = await OpenLoginFactory.getInstance(); + + if (reinitialize) { + await openLoginInstance.init(); + } + return openLoginInstance; + } finally { + releaseLock(); + } + } + async handleLoginWindow({ communicationEngine, communicationWindowManager, diff --git a/src/modules/auth.ts b/src/modules/auth.ts index f005f9ca..1d3a1871 100644 --- a/src/modules/auth.ts +++ b/src/modules/auth.ts @@ -1,12 +1,20 @@ import { computed, onMounted, watch } from "vue"; import { useRouter } from "vue-router"; +import OpenLoginHandler from "@/auth/OpenLoginHandler"; + import ControllerModule from "./controllers"; export function requireLoggedIn(): void { const router = useRouter(); - onMounted(() => { + onMounted(async () => { if (!ControllerModule.torus.selectedAddress) router.push("/login"); + const openLoginHandler = await OpenLoginHandler.getInstance(true); + const { sessionId } = openLoginHandler; + if (!sessionId) { + ControllerModule.logout(); + router.push("/login"); + } }); const currentAddress = computed(() => ControllerModule.torus.selectedAddress);