Add centralized API client with axios interceptors, remove vue-axios
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
33
src/api/auth.js
Normal file
33
src/api/auth.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import axios from 'axios';
|
||||
import { getCookie, setCookie, setCookieWithoutExpiration } from '@/utils/cookieUtil.js';
|
||||
|
||||
/**
|
||||
* Refresh the access token using the refresh token cookie.
|
||||
* Uses plain axios (not apiClient) to avoid interceptor loops.
|
||||
* @returns {Promise<string>} The new access token.
|
||||
*/
|
||||
export async function refreshTokenAndGetNew() {
|
||||
const api = '/api/oauth/token';
|
||||
const config = {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
};
|
||||
const data = {
|
||||
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);
|
||||
// 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);
|
||||
|
||||
return newAccessToken;
|
||||
}
|
||||
Reference in New Issue
Block a user