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);