Skip to content

Commit

Permalink
set token in header
Browse files Browse the repository at this point in the history
  • Loading branch information
sksabbirhossain committed Aug 2, 2023
1 parent 878897a commit 8e4d6dd
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/features/api/apiSlice.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
import { adminLoggedOut } from "../auth/authSlice";

const baseQuery = fetchBaseQuery({
baseUrl: process.env.REACT_APP_BASE_URL,
prepareHeaders: async (headers, { getState, endpoint }) => {
const token = getState()?.adminAuth?.accessToken;
if (token) {
headers.set("Authorization", `Bearer ${token}`);
}
return headers;
},
});

export const apiSlice = createApi({
reducerPath: "api",
baseQuery: fetchBaseQuery({
baseUrl: process.env.REACT_APP_BASE_URL,
}),
baseQuery: async (args, api, extraOptions) => {
let result = await baseQuery(args, api, extraOptions);
if (result?.error?.status === 500) {
api.dispatch(adminLoggedOut());
localStorage.clear();
}
return result;
},
tagTypes: [],
endpoints: (builder) => ({}),
});

0 comments on commit 8e4d6dd

Please sign in to comment.