I'm completely new to using RTK Query, I created the app before but without the authentication and everything worked, now I want to add the authentication using Auth0 but I can't access any file I add getTokenSilently()
PS. getTokenSilently is the {token}
thanks for help
export const myApi = createApi({
reducerPath: "points",
baseQuery: fetchBaseQuery({
baseUrl: "/",
prepareHeaders: (headers, { getState }) => {
const token = getState()
if (token) {
headers.Authorization = `Bearer ${token}`
}
return headers
},
}),
endpoints: builder => ({
getPoints: builder.query({
query: () => `/`,
}),
}),
})
export const { useGetPointsQuery } = myApi
.getTokenSilently()
return a promise. I am unsure how to resolve that promises in the context of RTK Query'screateApi()
. – WardieuprepareHeaders
an async function andawait getTokenSilently()
in atry
block to get the token. The problem here though is thatgetTokenSilently
comes from a hook and hooks only work inside a React functional component (which the slice is not) so it is not possible to get the token directly that way. You would need to set the token to some global state that you can access on the slice or use the auth0-spa library. – Magyar