如何将 RTK Query apiSlice 减速器与常规 RTK 减速器混合使用?

发布于 2025-01-12 04:03:35 字数 1555 浏览 3 评论 0原文

我正在将一些旧的 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

我有三个问题目前无法解决:

  1. 我想在浏览器中保留用户的身份验证令牌(在原始应用程序中使用 redux-persist 中间件 - 不确定如何在此处集成)
  2. 我需要 tokenRefresh 的结果来覆盖原始 tokenFetch api 调用(目前它们是独立存储的),并且
  3. 我需要处理tokenInvalidate 操作,带有一个reducer,可以简单地重置身份验证状态。 (我看不出如何将其他减速器注入到此 apiSlice 中)

如何确保 tokenFetchtokenRefresh 都将数据推送到 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:

  1. 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)
  2. I need the results of tokenRefresh to overwite the original tokenFetch api call, (currently they are stored independently) and
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2025-01-19 04:03:35

RTK 文档有一个 将身份验证令牌保存到一边的示例额外的减速器

我认为这显示了您想要的大部分内容 - 通过使用 extraReducersfulfilled 操作,将令牌保存到您可以随意操作的额外切片中的一侧>。

此外,它还展示了如何在 baseQueryprepareHeaders 中使用该令牌。

除此之外,您可能还需要 包装您的基本查询以进行自动重新身份验证

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 in extraReducers.

Also, it shows how to use that token in prepareHeaders of your baseQuery.

In addition to that, you might want to wrap your baseQuery for automatic re-authentication.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文