【问题标题】:devise_token_auth - No access-token in response headersdevise_token_auth - 响应标头中没有访问令牌
【发布时间】:2018-02-13 06:02:39
【问题描述】:

我在我的 rails 应用程序中使用 devise_token_auth,Ver 0.1.37。我正在使用 rspec 来测试我的应用程序。

应用登录成功,响应头返回access-token。在后续请求中,未收到新的访问令牌。请求成功,HTTP 状态为 201。

【问题讨论】:

  • 这是登录后发出的示例请求标头。 {"ACCEPT"=>"application/json", "Content-Type"=>"application/json", "access-token"=>"SYotATXiRVWJbkX_LR2TLw", "token-type"=>"Bearer", "client"=>"188dmqOWOLsAAzFVBD1vJg", "expiry"=>"1456358941", "uid"=>"vida@kihn.net"}
  • 可能是因为批量请求令牌没有更新?

标签: ruby-on-rails rspec


【解决方案1】:

一段时间以来,我一直在为这个问题苦苦挣扎,并在我的 React Native 项目中通过以下方式解决了它:

  • 确保我将 Rails 应用程序中的标头列入白名单(在 Rack CORS 配置文件中)

    config.middleware.insert_before 'Rack::Runtime', 'Rack::Cors' do allow do origins '*' resource '*', headers: :any, expose: ['access-token', 'expiry', 'token-type', 'uid', 'client'], methods: [:get, :put, :post, :patch, :delete, :options] end end

  • 保存(使用 AsynStorage)我登录后返回的身份验证标头

    if (resp.ok) { if (resp.headers.get('access-token')) { let accessToken = resp.headers.get('access-token'); let client = resp.headers.get('client'); let expiry = resp.headers.get('expiry'); let tokenType = resp.headers.get('token-type'); let uid = resp.headers.get('uid'); let authHeader = {'client': client, expiry: expiry, uid: uid, 'access-token': accessToken, 'token-type': tokenType}; AsyncStorage.setItem('auth_header', JSON.stringify(authHeader)); } return json; }

  • 为每个后续请求使用保存的标头

    export function fetchTheThing() { return (dispatch, getState) => { return AsyncStorage.getItem('auth_header', (err, result) => { return Api.get(/the_thing, null, result).then(resp => { dispatch(setFetchedThing({ thing: resp })); }).catch((ex) => { console.log(ex); }); }); }; }

我也遇到了重置每个请求的令牌的默认设置的问题,所以我在 devise_token_auth.rb 中将其设置为 false。

(对不起那些代码块的格式)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2011-03-07
    • 2017-09-28
    • 2015-09-27
    • 2021-07-29
    相关资源
    最近更新 更多