He intentado implementar la reautorización automática, pero cada vez que intento acceder al punto final de la API ("/refresh") para obtener un nuevo token de acceso, me devuelve este error.
error: data: "Forbidden" error: "SyntaxError: Unexpected token F in JSON at position 0" originalStatus: 403 status: "PARSING_ERROR". Intenté hacer lo mismo con axios y funcionó, devolvió el token de acceso, el punto final incluso funciona cuando también uso el cliente Api de insomnio.
Aquí está mi código
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' import { setCredentials, logOut } from '../component-slice' const baseQuery = fetchBaseQuery({ baseUrl: "http://localhost:5000/api", credentials: 'include', prepareHeaders: (headers, { getState }) => { const token = getState().componentSlice.token if (token) { headers.set('authorization', `Bearer ${token}`) } return headers } }) const baseQueryWithReauth = async (args, api, extraOptions) => { let result = await baseQuery(args, api, extraOptions) if (result?.error?.originalStatus === 403) { console.log('sending refresh token') // send refresh token to get new access token const refreshResult = await baseQuery('/refresh', api, extraOptions) console.log(refreshResult) if (refreshResult?.data) { const user = api.getState().componentSlice.user // store the new token api.dispatch(setCredentials({ ...refreshResult.data, user })) // retry the original query with new access token result = await baseQuery(args, api, extraOptions) } else { api.dispatch(logOut()) } } return result } export const userApi = createApi({ reducerPath: 'api', baseQuery: baseQueryWithReauth, tagTypes: ['Product', 'List', 'category'], endpoints: builder => ({ getProfile: builder.mutation({ query: () => ({ url: `/profile`, credentials: 'include', method: 'GET' }) }), googleLogout: builder.mutation({ query: () => ({ url: `/google/logout`, method: 'GET', credentials: 'include' }) }) }) })