简体   繁体   中英

how to properly authenticate user with devise_token_auth rails gem?

I am trying to write a simple test for a URL which should be visible only to logged in user (I use before_action :authenticate_user! to achieve this).

Even though I (think) that I properly assign all the headers I always get JSON response {"errors":["Authorized users only."]}

My test looks like this (Login (to get header info) then visit protected URL with all the headers needed)

post "/api/v1/auth/sign_in", {:email => user.email, password: 'password'}

header_hash = {
    'access-token'  => response.headers['access-token'],
    'uid'           => response.headers['uid'],
    'client'        => response.headers['client'],
    'expiry'        => response.headers['expiry']
}

get "/api/v1/subscription_status", nil , header_hash

What am I doing wrong?

I didn't know that authentication token is changing on each request by default.

conf/device_token_auth.rb

config.change_headers_on_each_request = false

I set this setting to false and afterwards I got responses which I expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM