在react-native中使用devise_token_auth获取

问题描述 投票:0回答:1

我是反应原生的新手。我试图满足在每个请求中发送身份验证标头的 devise_token_auth 要求。为此,我正在尝试这样的事情:

export const scheduleFetch = (auth) => {
  return (dispatch) => {
    fetch(URL, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json; charset=utf-8',
        'access-token': auth['acessToken'],
        'token-type': auth['tokenType'],
        'client': auth['client'],
        'uid': auth['uid']
      }
    })
    .then((response) => {
      console.log(response)
      response.json()
    })
    .catch((error) => console.log(error))
  }
}

我的后端正在接收请求,所有标头均已填充。然而,我仍然收到消息

"_bodyText":"{\"errors\":[\"You need to sign in or sign up before continuing.\"]}"

我怎样才能做到这一点?我有跳过任何一步吗?

react-native devise rails-api
1个回答
0
投票

响应很晚,但以防万一其他人寻找答案,devise_token_auth 默认情况下会更改每个请求的访问令牌(change_headers_on_each_request = true)。因此,您不仅需要在每个请求中发送身份验证令牌,还需要在每个响应上保存新的身份验证令牌。您可以通过在 config/initializers/devise_token_auth.rb 中的初始化程序设置中将其设置为 false 来关闭此功能,在这种情况下,访问令牌不会更改,但安全性会降低。

© www.soinside.com 2019 - 2024. All rights reserved.