2022-01-12 19:53:45 +08:00
|
|
|
import type { AxiosRequestConfig } from 'axios';
|
|
|
|
|
import { useAuthStore } from '@/store';
|
|
|
|
|
import { getRefreshToken, setToken, setRefreshToken } from '@/utils';
|
|
|
|
|
import { fetchUpdateToken } from '../api';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 刷新token
|
2022-01-16 20:13:11 +08:00
|
|
|
* @param axiosConfig - token失效时的请求配置
|
2022-01-12 19:53:45 +08:00
|
|
|
*/
|
|
|
|
|
export async function refreshToken(axiosConfig: AxiosRequestConfig) {
|
|
|
|
|
const { resetAuthStore } = useAuthStore();
|
2022-03-12 16:21:40 +08:00
|
|
|
const rToken = getRefreshToken();
|
|
|
|
|
const { data } = await fetchUpdateToken(rToken);
|
2022-01-12 19:53:45 +08:00
|
|
|
if (data) {
|
|
|
|
|
setToken(data.token);
|
|
|
|
|
setRefreshToken(data.refreshToken);
|
|
|
|
|
const config = { ...axiosConfig };
|
|
|
|
|
if (config.headers) {
|
|
|
|
|
config.headers.Authorization = data.token;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-16 20:13:11 +08:00
|
|
|
resetAuthStore();
|
2022-01-12 19:53:45 +08:00
|
|
|
return null;
|
|
|
|
|
}
|