From 9a8b45b1514aab5fdd3c226f09eac3bbedd4b017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sat, 7 Mar 2026 09:21:25 +0800 Subject: [PATCH] Deduplicate refreshToken by delegating to auth.js refreshTokenAndGetNew Co-Authored-By: Claude Opus 4.6 --- src/stores/login.ts | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/stores/login.ts b/src/stores/login.ts index 49b3ea0..605f5ab 100644 --- a/src/stores/login.ts +++ b/src/stores/login.ts @@ -13,7 +13,7 @@ import { defineStore } from "pinia"; import axios from 'axios'; import apiClient from '@/api/client.js'; import apiError from '@/module/apiError.js'; -import { deleteCookie, setCookie, setCookieWithoutExpiration, getCookie } from "../utils/cookieUtil"; +import { deleteCookie, setCookie, setCookieWithoutExpiration } from "../utils/cookieUtil"; /** Pinia store for authentication and user session management. */ export const useLoginStore = defineStore('loginStore', { @@ -78,27 +78,10 @@ export const useLoginStore = defineStore('loginStore', { * Refresh Token */ async refreshToken() { - const api = '/api/oauth/token'; - const config = { - headers: { - 'Content-Type':'application/x-www-form-urlencoded', - }, - }; - - this.auth.grant_type = 'refresh_token'; - this.auth.refresh_token = getCookie("luciaRefreshToken"); - try { - const response = await axios.post(api, this.auth, config); - if(response.status === 200) { - const newAccessToken = response.data.access_token; - const newRefreshToken = response.data.refresh_token; - - setCookieWithoutExpiration("luciaToken", newAccessToken); - setCookie("luciaRefreshToken", newRefreshToken, Math.ceil((this.expired - Date.now()) / (24 * 60 * 60 * 1000))); - } + const { refreshTokenAndGetNew } = await import('@/api/auth.js'); + await refreshTokenAndGetNew(); } catch(error) { - // 若refresh token 失敗則導向至登入頁面 this.$router.push('/login'); throw error; }