Skip to content

Commit

Permalink
[BUG] Attach auth headers for rtk client endpoints #129: API fetch fu…
Browse files Browse the repository at this point in the history
…nction to include authorization header
  • Loading branch information
ballyalley-o committed Apr 14, 2024
1 parent 2b21a91 commit c61e859
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/store/slice/api.ts
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) => ({})
})

0 comments on commit c61e859

Please sign in to comment.