mirror of
https://github.com/m-xlsea/ruoyi-plus-soybean.git
synced 2025-09-24 07:49:47 +08:00
feat(projects): 请求拦截器添加刷新token
This commit is contained in:
26
src/service/request/helpers.ts
Normal file
26
src/service/request/helpers.ts
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user