46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { fetchBaseQuery, type BaseQueryFn, type FetchArgs, type FetchBaseQueryError } from '@reduxjs/toolkit/query/react';
|
|
import type { RootState } from '../store';
|
|
import { getApiBaseUrl } from '../config/runtime-config';
|
|
import { logout, setTokens } from '../store/slices/auth';
|
|
|
|
const rawBaseQuery = () =>
|
|
fetchBaseQuery({
|
|
baseUrl: getApiBaseUrl(),
|
|
prepareHeaders: (headers, { getState }) => {
|
|
const token = (getState() as RootState).auth.accessToken;
|
|
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
return headers;
|
|
},
|
|
});
|
|
|
|
export const baseQueryWithReauth: BaseQueryFn<string | FetchArgs, unknown, FetchBaseQueryError> =
|
|
async (args, api, extraOptions) => {
|
|
let result = await rawBaseQuery()(args, api, extraOptions);
|
|
|
|
if (result.error?.status === 401) {
|
|
const state = api.getState() as RootState;
|
|
const refreshToken = state.auth.refreshToken;
|
|
|
|
if (refreshToken) {
|
|
const refreshResult = await rawBaseQuery()({
|
|
url: '/auth/refresh',
|
|
method: 'POST',
|
|
body: { refreshToken },
|
|
}, api, extraOptions);
|
|
|
|
if (refreshResult.data) {
|
|
const { accessToken, refreshToken: newRefresh, expiresIn } =
|
|
(refreshResult.data as { accessToken: string; refreshToken: string; expiresIn: number });
|
|
api.dispatch(setTokens({ accessToken, refreshToken: newRefresh, expiresIn }));
|
|
result = await rawBaseQuery()(args, api, extraOptions);
|
|
} else {
|
|
api.dispatch(logout());
|
|
}
|
|
} else {
|
|
api.dispatch(logout());
|
|
}
|
|
}
|
|
|
|
return result;
|
|
};
|