From 202b6c1a104e56634cadb05892ed46dfd0825f3d Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Fri, 5 Apr 2024 21:48:53 +0800 Subject: [PATCH] :bug: Fix infinite reconnect issue --- src/scripts/request.ts | 48 ++++++++++++++++++++----------------- src/stores/channels.ts | 11 +++++---- src/stores/notifications.ts | 11 +++++---- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/src/scripts/request.ts b/src/scripts/request.ts index 24bf599..7d4ee96 100644 --- a/src/scripts/request.ts +++ b/src/scripts/request.ts @@ -7,33 +7,37 @@ const serviceMap: { [id: string]: string } = { messaging: "https://im.solsynth.dev" } +export async function refreshToken() { + const res = await request("identity", "/api/auth/token", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + refresh_token: await getRtk(), + grant_type: "refresh_token" + }) + }, true) + if (res.status !== 200) { + const err = await res.text() + throw new Error(err) + } else { + const data = await res.json() + await Preferences.set({ + key: "identity.access_token", + value: data["access_token"] + }) + await Preferences.set({ + key: "identity.refresh_token", + value: data["refresh_token"] + }) + } +} + export async function request(service: string, input: string, init?: RequestInit, noRetry?: boolean) { const url = buildRequestUrl(service, input) const res = await fetch(url, init) if (res.status === 401 && !noRetry) { - const res = await request("identity", "/api/auth/token", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - refresh_token: await getRtk(), - grant_type: "refresh_token" - }) - }, true) - if (res.status !== 200) { - const err = await res.text() - throw new Error(err) - } else { - const data = await res.json() - await Preferences.set({ - key: "identity.access_token", - value: data["access_token"] - }) - await Preferences.set({ - key: "identity.refresh_token", - value: data["refresh_token"] - }) - } + await refreshToken() console.info("[REQUEST] Auth context has been refreshed.") return await request(service, input, { diff --git a/src/stores/channels.ts b/src/stores/channels.ts index d90b06f..cb90fe2 100644 --- a/src/stores/channels.ts +++ b/src/stores/channels.ts @@ -1,7 +1,7 @@ import { defineStore } from "pinia" import { reactive, ref, watch } from "vue" import { checkLoggedIn, getAtk } from "@/stores/userinfo" -import { buildRequestUrl, request } from "@/scripts/request" +import { buildRequestUrl, refreshToken, request } from "@/scripts/request" import { useRoute } from "vue-router" import { useUI } from "@/stores/ui" @@ -73,15 +73,16 @@ export const useChannels = defineStore("channels", () => { socket.addEventListener("open", (event) => { console.log("[MESSAGING] The unified websocket has been established... ", event.type) - reconnectCount = 0 }) socket.addEventListener("close", (event) => { console.warn("[MESSAGING] The unified websocket is disconnected... ", event.reason, event.code) const reconnect = () => { reconnectCount = 0 - connect().then(() => { - ui.reconnection.messages = false - reconnectCount++ + refreshToken().then(() => { + connect().then(() => { + ui.reconnection.messages = false + reconnectCount++ + }) }) } ui.reconnection.messages = true diff --git a/src/stores/notifications.ts b/src/stores/notifications.ts index b382669..e407962 100644 --- a/src/stores/notifications.ts +++ b/src/stores/notifications.ts @@ -1,7 +1,7 @@ import { defineStore } from "pinia" import { ref } from "vue" import { checkLoggedIn, getAtk } from "@/stores/userinfo" -import { buildRequestUrl, request } from "@/scripts/request" +import { buildRequestUrl, refreshToken, request } from "@/scripts/request" import { LocalNotifications } from "@capacitor/local-notifications" import { Capacitor } from "@capacitor/core" import { useUI } from "@/stores/ui" @@ -52,15 +52,16 @@ export const useNotifications = defineStore("notifications", () => { socket.addEventListener("open", (event) => { console.log("[NOTIFICATIONS] The listen websocket has been established... ", event.type) - reconnectCount = 0 }) socket.addEventListener("close", (event) => { console.warn("[NOTIFICATIONS] The listen websocket is disconnected... ", event.reason, event.code) const reconnect = () => { reconnectCount = 0 - connect().then(() => { - ui.reconnection.notifications = false - reconnectCount++ + refreshToken().then(() => { + connect().then(() => { + ui.reconnection.notifications = false + reconnectCount++ + }) }) } ui.reconnection.notifications = true