-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUG] Attach auth headers for rtk client endpoints #129: API fetch fu…
…nction to include authorization header
- Loading branch information
1 parent
2b21a91
commit c61e859
Showing
1 changed file
with
18 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react' | ||
import { createApi, fetchBaseQuery, BaseQueryApi, FetchArgs } from '@reduxjs/toolkit/query/react' | ||
import { ServerPath } from 'route/path' | ||
import { getAuthToken } from 'auth/utility' | ||
|
||
const baseQuery = fetchBaseQuery({ | ||
baseUrl: ServerPath.SERVER | ||
}) | ||
const extFetchBase = (args: FetchArgs, api: BaseQueryApi, extraOptions: any, authToken: string) => { | ||
const headers = { | ||
Authorization: `Bearer ${authToken}` | ||
} | ||
|
||
return fetchBaseQuery({ | ||
baseUrl: ServerPath.SERVER, | ||
prepareHeaders: (headers, { getState }) => { | ||
return headers | ||
} | ||
})(args, api, { | ||
...extraOptions, | ||
headers | ||
}) | ||
} | ||
|
||
export const apiSlice = createApi({ | ||
baseQuery, | ||
baseQuery: (args, api, extraOptions) => extFetchBase(args, api, extraOptions, getAuthToken() as string), | ||
tagTypes: ['Bootcamp', 'Course', 'Feedback', 'User'], | ||
endpoints: (builder) => ({}) | ||
}) |