[英]Rails gem devise_token_auth headers in web console are enoguh to access api, why?
[英]Devise Token Auth - How to access a protected route from the Rails console
我正在使用Angularjs,Rails,devise_token_auth和ng-token-auth,並且試圖從控制台登錄用戶。
我不知道如何獲得蒂姆·桑特福德在本文中提到的302。
這仍然適用於Rails 4還是200響應仍然表示我沒有成功登錄?
我做了Brian Deterling提到的發帖請求,得到了:
Started POST "/api/private/auth/sign_in" for 127.0.0.1 at 2016-03-30 13:36:44 -0500
Processing by DeviseTokenAuth::SessionsController#create as JSON
Parameters: {"email"=>"freud@iron.com", "password"=>"[FILTERED]"}
...
(9.9ms) COMMIT
Completed 200 OK in 361ms (Views: 0.4ms | ActiveRecord: 23.3ms)
=> 200
因此,除了200而不是Tim Santeford提到的302響應之外,我似乎已成功登錄。
然后,我執行app.session,它看起來像工作。
irb(main):026:0> app.session
=> #<ActionDispatch::Request::Session:0x007fe7a1898358 @by=#<ActionDispatch::Session::CookieStore:0x007fe79ed821f0
...
@delegate={"session_id"=>"4346eb0ee9fc8a8e95735d1e3f2adc28"}, @loaded=true, @exists=true>
然后,我執行一個app.controller.current_user,這看起來也可以使用。
irb(main):027:0> app.controller.current_user
=> #<User id: 233, provider: "email", uid: "freud@iron.com", encrypted_password: "$2a$10$v2fapiZCH5b5XN.ji57KgOJ0SJfVHzJByYPFpVCeU1j...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 32, current_sign_in_at: "2016-03-30 18:36:44", last_sign_in_at: "2016-03-30 18:01:00", current_sign_in_ip: "127.0.0.1",
但是當我輸入受保護的路由時,我得到
irb(main):029:0> app.get('/api/private/rfqs')
Started GET "/api/private/rfqs" for 127.0.0.1 at 2016-03-30 14:11:21 -0500
Processing by Api::RfqsController#index as JSON
Filter chain halted as :authenticate_user! rendered or redirected
Completed 401 Unauthorized in 1ms (Views: 0.2ms | ActiveRecord: 0.0ms)
=> 401
...如果在此請求之后,我再次執行app.session和app.controller.current_user
irb(main):030:0> app.session
=> #<ActionDispatch::Request::Session:0x7fe7a1213910 not yet loaded>
irb(main):031:0> app.controller.current_user
=> nil
有人可以告訴我為什么會這樣嗎?
首先,您應該知道Devise
不再管理用戶的會話。 這就是為什么建議使用devise_token_auth
。 另外,由於您使用的是devise_token_auth
gem,因此所引用的答案也不是為您提供幫助的理想選擇。
錯誤Filter chain halted as :authenticate_user! rendered or redirected
Filter chain halted as :authenticate_user! rendered or redirected
意味着您必須登錄才能運行Api::RfqsController#index
控制器操作。 登錄時,您在headers
收到以下信息:
"access-token": "my-access-token",
"token-type": "Bearer",
"client": "my-client-id",
"expiry": "yyyyy",
"uid": "freud@iron.com"
為了使app.get('/api/private/rfqs')
起作用,您需要添加查詢標頭:
access-token
uid
token-type
(值為Bearer
) client
我建議您使用Postman
軟件測試您的Rails API。 比使用控制台更有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.