Skip to content

Commit

Permalink
fix: session logout
Browse files Browse the repository at this point in the history
  • Loading branch information
guru-web3 committed Jul 4, 2023
1 parent 2f6152a commit cca08d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/auth/OpenLoginHandler.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion src/modules/auth.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit cca08d3

Please sign in to comment.