【问题标题】:Auth Bearer Token in Redux SagaRedux Saga 中的 Auth Bearer Token
【发布时间】:2020-12-17 21:04:16
【问题描述】:

我正在尝试在我的 API 中使用从这个传奇返回的 Bearer 令牌进行身份验证,并且我想将它传递给 Authorization HTTP 标头,有人知道该怎么做吗?谢谢。

import { takeLatest, call, put, all } from 'redux-saga/effects';
import { apiCore } from '@/services/api';
import { ActionType } from 'typesafe-actions';
import * as actions from './actions';

export function* signIn(action: ActionType<typeof actions.signInRequest>) {
  try {
    const { email, password } = action.payload;
    const { data } = yield call(apiCore.post, '/auth/myapp/login', {
      email,
      password
    });

    yield put(actions.signInSuccess({ token: data.result.token }));

    **yield call(apiCore.get, '', PASS THE TOKEN SOMEHOW? (data.result.token))????**

  } catch (err) {
    yield put(actions.signInFailure());
  }
}

export default all([takeLatest('@login/SIGN_IN_REQUEST', signIn)]);

【问题讨论】:

    标签: reactjs http redux axios redux-saga


    【解决方案1】:

    您可以简单地将参数作为第二个参数传递给call() 函数。

    如文档中所述:

    调用(fn, ...args)

    创建一个 Effect 描述,指示中间件以 args 作为参数调用函数 >fn。

    fn: Function - 一个生成器函数,或者返回一个 >Promise 作为结果的普通函数,或者任何其他值。

    args: Array - 要作为参数传递给 fn 的值数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2020-11-12
      • 2021-07-20
      • 1970-01-01
      • 2021-01-05
      • 2017-07-07
      • 1970-01-01
      相关资源
      最近更新 更多