fossil
fossil

Reputation: 770

devise_token_auth - No access-token in response headers

I am using devise_token_auth, Ver 0.1.37, in my rails application. I am using rspec to test my application.

The sign-in to application is successful and access-token is returned in response header. On subsequent request, the new access token is not received. The request was successful with a HTTP status 201.

Upvotes: 2

Views: 1225

Answers (1)

Skip Suva
Skip Suva

Reputation: 935

I was beating my head against the wall with this issue for some time, and solved it in my React Native project by:

  • ensuring I was whitelisting the headers in my rails app (in the Rack CORS config file)

    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

  • saving (with AsynStorage) the auth headers I got back after logging in

    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; }

  • using the saved headers for each subsequent requests

    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); }); }); }; }

I was also running into issue with the default setting that resets the token each request, so I set that to false in the devise_token_auth.rb.

(sorry for the formatting of those code blocks)

Upvotes: 1

Related Questions