Deduplicate refreshToken by delegating to auth.js refreshTokenAndGetNew
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user