import { fetchBaseQuery } from '@reduxjs/toolkit/query' import { tokenReceived, loggedOut } from './authSlice' const baseQuery = fetchBaseQuery({ baseUrl: '/' }) const baseQueryWithReauth = async (args, api, extraOptions) => { let result = await baseQuery(args, api, extraOptions) if (result.error && result.error.status === 401) { // try to get a new token const refreshResult = await baseQuery('/refreshToken', api, extraOptions) if (refreshResult.data) { // store the new token api.dispatch(tokenReceived(refreshResult.data)) // retry the initial query result = await baseQuery(args, api, extraOptions) } else { api.dispatch(loggedOut()) } } return result }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter