Search code examples
reactjsreact-nativestorerefluxjsredux-saga

Redux use token in every API request


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.


Solution

  • 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.