Question: Redux use token in every API request


Redux use token in every API request

Answers 2
Added at 2016-08-29 01:08

I am trying to develop an app using React Native, Redux and Redux Saga.

I have the following login flow:

  1. Login Form
  2. Login Attempt Action
  3. Login Saga
  4. API Call
  5. Login Reducer stores auth token

I have multiple containers and components which each execute actions which in turn have sagas doing API calls. What would be the correct way to pass the token from the store to the API calling method while keeping the code clean and reusable.

Answers to

Redux use token in every API request

nr: #1 dodano: 2016-08-29 02:08

My solution for this: wrap fetch() with your own function and export it. Connect your new fetch with redux and use it in sagas.

nr: #2 dodano: 2016-08-29 04:08

You can create API saga(s) which will process all API actions from other app sagas. Use select effect here to get token from the store and pass it to API Call. Keep API Call separated from this saga for more abstraction and DRY principle. There are several options to call this saga: direct with yield* or by throwing redux action.

Source Show
◀ Wstecz