feat(projects): 请求拦截器添加刷新token

This commit is contained in:
Soybean
2022-01-12 19:53:45 +08:00
parent 09c7658c21
commit 839b82ba8b
8 changed files with 213 additions and 126 deletions

View File

@ -0,0 +1,26 @@
import type { AxiosRequestConfig } from 'axios';
import { useAuthStore } from '@/store';
import { getRefreshToken, setToken, setRefreshToken } from '@/utils';
import { fetchUpdateToken } from '../api';
/**
* 刷新token
* token失效时的请求配置
*/
export async function refreshToken(axiosConfig: AxiosRequestConfig) {
const { resetAuthStore } = useAuthStore();
const refreshToken = getRefreshToken();
const { data } = await fetchUpdateToken(refreshToken);
if (data) {
setToken(data.token);
setRefreshToken(data.refreshToken);
const config = { ...axiosConfig };
if (config.headers) {
config.headers.Authorization = data.token;
}
return config;
}
resetAuthStore(true);
return null;
}