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 axios from 'axios';
|
||||||
import apiClient from '@/api/client.js';
|
import apiClient from '@/api/client.js';
|
||||||
import apiError from '@/module/apiError.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. */
|
/** Pinia store for authentication and user session management. */
|
||||||
export const useLoginStore = defineStore('loginStore', {
|
export const useLoginStore = defineStore('loginStore', {
|
||||||
@@ -78,27 +78,10 @@ export const useLoginStore = defineStore('loginStore', {
|
|||||||
* Refresh Token
|
* Refresh Token
|
||||||
*/
|
*/
|
||||||
async refreshToken() {
|
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 {
|
try {
|
||||||
const response = await axios.post(api, this.auth, config);
|
const { refreshTokenAndGetNew } = await import('@/api/auth.js');
|
||||||
if(response.status === 200) {
|
await refreshTokenAndGetNew();
|
||||||
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)));
|
|
||||||
}
|
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
// 若refresh token 失敗則導向至登入頁面
|
|
||||||
this.$router.push('/login');
|
this.$router.push('/login');
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user