Apply repository-wide ESLint auto-fix formatting pass
Co-Authored-By: Codex <codex@openai.com>
This commit is contained in:
@@ -4,8 +4,12 @@
|
||||
// imacat.yang@dsp.im (imacat), 2023/9/23
|
||||
/** @module auth Authentication token refresh utilities. */
|
||||
|
||||
import axios from 'axios';
|
||||
import { getCookie, setCookie, setCookieWithoutExpiration } from '@/utils/cookieUtil.js';
|
||||
import axios from "axios";
|
||||
import {
|
||||
getCookie,
|
||||
setCookie,
|
||||
setCookieWithoutExpiration,
|
||||
} from "@/utils/cookieUtil.js";
|
||||
|
||||
/**
|
||||
* Refreshes the access token using the stored refresh token cookie.
|
||||
@@ -18,27 +22,29 @@ import { getCookie, setCookie, setCookieWithoutExpiration } from '@/utils/cookie
|
||||
* @throws {Error} If the refresh request fails.
|
||||
*/
|
||||
export async function refreshTokenAndGetNew() {
|
||||
const api = '/api/oauth/token';
|
||||
const api = "/api/oauth/token";
|
||||
const config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
};
|
||||
const data = {
|
||||
grant_type: 'refresh_token',
|
||||
refresh_token: getCookie('luciaRefreshToken'),
|
||||
grant_type: "refresh_token",
|
||||
refresh_token: getCookie("luciaRefreshToken"),
|
||||
};
|
||||
|
||||
const response = await axios.post(api, data, config);
|
||||
const newAccessToken = response.data.access_token;
|
||||
const newRefreshToken = response.data.refresh_token;
|
||||
|
||||
setCookieWithoutExpiration('luciaToken', newAccessToken);
|
||||
setCookieWithoutExpiration("luciaToken", newAccessToken);
|
||||
// Expire in ~6 months
|
||||
const expiredMs = new Date();
|
||||
expiredMs.setMonth(expiredMs.getMonth() + 6);
|
||||
const days = Math.ceil((expiredMs.getTime() - Date.now()) / (24 * 60 * 60 * 1000));
|
||||
setCookie('luciaRefreshToken', newRefreshToken, days);
|
||||
const days = Math.ceil(
|
||||
(expiredMs.getTime() - Date.now()) / (24 * 60 * 60 * 1000),
|
||||
);
|
||||
setCookie("luciaRefreshToken", newRefreshToken, days);
|
||||
|
||||
return newAccessToken;
|
||||
}
|
||||
|
||||
@@ -8,15 +8,15 @@
|
||||
* 401 token refresh with request queuing.
|
||||
*/
|
||||
|
||||
import axios from 'axios';
|
||||
import { getCookie, deleteCookie } from '@/utils/cookieUtil.js';
|
||||
import axios from "axios";
|
||||
import { getCookie, deleteCookie } from "@/utils/cookieUtil.js";
|
||||
|
||||
/** Axios instance configured with auth interceptors. */
|
||||
const apiClient = axios.create();
|
||||
|
||||
// Request interceptor: automatically attach Authorization header
|
||||
apiClient.interceptors.request.use((config) => {
|
||||
const token = getCookie('luciaToken');
|
||||
const token = getCookie("luciaToken");
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
@@ -54,7 +54,7 @@ apiClient.interceptors.response.use(
|
||||
if (
|
||||
error.response?.status !== 401 ||
|
||||
originalRequest._retried ||
|
||||
originalRequest.url === '/api/oauth/token'
|
||||
originalRequest.url === "/api/oauth/token"
|
||||
) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
@@ -76,7 +76,7 @@ apiClient.interceptors.response.use(
|
||||
|
||||
try {
|
||||
// Dynamic import to avoid circular dependency with login store
|
||||
const { refreshTokenAndGetNew } = await import('@/api/auth.js');
|
||||
const { refreshTokenAndGetNew } = await import("@/api/auth.js");
|
||||
const newToken = await refreshTokenAndGetNew();
|
||||
isRefreshing = false;
|
||||
onRefreshSuccess(newToken);
|
||||
@@ -87,11 +87,11 @@ apiClient.interceptors.response.use(
|
||||
onRefreshFailure(refreshError);
|
||||
|
||||
// Refresh failed: clear auth and redirect to login
|
||||
deleteCookie('luciaToken');
|
||||
window.location.href = '/login';
|
||||
deleteCookie("luciaToken");
|
||||
window.location.href = "/login";
|
||||
return Promise.reject(refreshError);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
export default apiClient;
|
||||
|
||||
Reference in New Issue
Block a user