How can i get Authorization key from header in RTK Query

Viewed 413

I use RTK Query api for login, when the login is done with success, i get the token through the response header.

Is there any chance to access the 'Authorization' from the response header. I try it to build an custom baseQuery but i failed.

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { ILoginUser } from "./types";

const api = process.env.REACT_APP_BASEURL_DEV;

export const authApi = createApi({
  reducerPath: "authApi",
  baseQuery: fetchBaseQuery({
    baseUrl: `${api}`,
  }),
  endpoints: (builder) => ({
    signIn: builder.mutation({
      query: (body: ILoginUser) => {
        return {
          url: "/login",
          method: "post",
          body,
        };
      },
    }),
  }),
});

export const { useSignInMutation } = authApi;
1 Answers

If you want to access the header you can use transform response it need to be in mutation scope

Ex.

transformResponse(response,meta,error)=> console.log(meta.response.headers)
Related