RTKQuery Interceptors. How can I change method of the baseQuery to make POST the request with refresh token to update access token, instead of GET one. Solution below didn't help me. #4077
Replies: 2 comments 1 reply
-
| baseQuery(
  {
    url: "/auth/refresh_token",
    method: "POST",
    body: JSON.stringify({ refresh_token: refreshToken }),
  },
  api,
  extraOptions
); | 
Beta Was this translation helpful? Give feedback.
                  
                    1 reply
                  
                
            
      Answer selected by
        7R-Berehovskyi-Markiian
-
| And` how to set refresh token in headers.   if (result.error && result.error.status === 401) {
    const guestFlow = api.getState()?.auth?.guestFlow;
    const refreshToken = api.getState()?.auth?.refreshToken;
    console.log('refreshToken:-', refreshToken);
    // try to get a new token
    const refreshResult = await baseQuery(
      {
        url: 'auth/refresh-token',
        body: {},
        method: 'POST',
        headers: {
          Authorization: `Bearer ${refreshToken}`,
          // Accept: 'application/json',
          // 'Content-type': 'application/json',
        },
      },
      api,
      extraOptions,
    );
    const responseData = refreshResult?.data;
    if (responseData) {
      api.dispatch(setToken(responseData?.access_token));
      api.dispatch(setRefreshToken(responseData?.refresh_token));
      // retry the initial query
      result = await baseQuery(args, api, extraOptions);
    } else {
      if (!guestFlow) {
        storage.delete('appleAuth');
        api.dispatch(setClearStore());
        changeStack('AuthStack');
      }
    }
  } | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions