如何将 RTK Query apiSlice 减速器与常规 RTK 减速器混合使用?
我正在将一些旧的 Redux 操作从原来的定制应用程序迁移到 RTK 查询,但我遇到了一个问题,这可能只是我对 RTK 查询缺乏了解。
我有一个 apiSlice 如下
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { AUTH_SERVER, CLIENT_ID, REDIRECT_URI } from 'app/config'
const api = createApi({
reducerPath: 'auth',
baseQuery: fetchBaseQuery({
baseUrl: `https://${AUTH_SERVER}`
}),
endpoints: builder => ({
tokenFetch: builder.query({
query: ({ code, code_verifier }) => ({
url: '/oauth/token',
method: 'POST',
body: {
grant_type: 'authorization_code',
client_id: CLIENT_ID,
code_verifier,
code,
redirect_uri: REDIRECT_URI
}
})
}),
tokenRefresh: builder.query({
query: ({ refresh_token }) => ({
url: '/oauth/token',
method: 'POST',
body: {
grant_type: 'refresh_token',
client_id: CLIENT_ID,
refresh_token
}
})
})
})
})
export const { useTokenFetchQuery, useTokenRefreshQuery } = api
export default api
我有三个问题目前无法解决:
- 我想在浏览器中保留用户的身份验证令牌(在原始应用程序中使用 redux-persist 中间件 - 不确定如何在此处集成)
- 我需要
tokenRefresh
的结果来覆盖原始tokenFetch
api 调用(目前它们是独立存储的),并且 - 我需要处理
tokenInvalidate
操作,带有一个reducer,可以简单地重置身份验证状态。 (我看不出如何将其他减速器注入到此 apiSlice 中)
如何确保 tokenFetch
和 tokenRefresh
都将数据推送到 redux 状态的同一部分,除了 api 调用之外,如何注入一个减速器来擦除该状态?
I'm migrating some of our old Redux actions over from our original bespoke app to RTK Query and I've run into an issue which is likely just my lack of understanding of RTK Query.
I have an apiSlice
as follows
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { AUTH_SERVER, CLIENT_ID, REDIRECT_URI } from 'app/config'
const api = createApi({
reducerPath: 'auth',
baseQuery: fetchBaseQuery({
baseUrl: `https://${AUTH_SERVER}`
}),
endpoints: builder => ({
tokenFetch: builder.query({
query: ({ code, code_verifier }) => ({
url: '/oauth/token',
method: 'POST',
body: {
grant_type: 'authorization_code',
client_id: CLIENT_ID,
code_verifier,
code,
redirect_uri: REDIRECT_URI
}
})
}),
tokenRefresh: builder.query({
query: ({ refresh_token }) => ({
url: '/oauth/token',
method: 'POST',
body: {
grant_type: 'refresh_token',
client_id: CLIENT_ID,
refresh_token
}
})
})
})
})
export const { useTokenFetchQuery, useTokenRefreshQuery } = api
export default api
I have three issues I can't currently figure out:
- I want to persist the user's auth tokens in the brower, (using redux-persist middleware in the original app - not sure how to integrate that here)
- I need the results of
tokenRefresh
to overwite the originaltokenFetch
api call, (currently they are stored independently) and - I need to handle a
tokenInvalidate
action with a reducer that simply resets the auth state. (I can't see how I'd inject other reducers into this apiSlice)
How do I ensure that tokenFetch
and tokenRefresh
both push data to the same part of the redux state, and how do I inject a reducer in addition to the api calls to erase that state?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RTK 文档有一个 将身份验证令牌保存到一边的示例额外的减速器。
我认为这显示了您想要的大部分内容 - 通过使用
extraReducers
fulfilled 操作,将令牌保存到您可以随意操作的额外切片中的一侧>。此外,它还展示了如何在
baseQuery
的prepareHeaders
中使用该令牌。除此之外,您可能还需要 包装您的基本查询以进行自动重新身份验证。
The RTK docs have an example of saving an auth token to the side with extraReducers.
I think that shows most of what you want - saving the token to the side in an extra slice that you can manipulate at will, by using the
fulfilled
action of your endpoint inextraReducers
.Also, it shows how to use that token in
prepareHeaders
of yourbaseQuery
.In addition to that, you might want to wrap your baseQuery for automatic re-authentication.