3
我有一個API需要根據是否有登錄用戶返回結果。我在rails側使用devise_token_auth,在客戶端使用ng_token_auth(Angular)。devise_token_auth - 是否可以authenticate_user!沒有失敗的過濾器鏈?
在我的控制,我想這樣做如下:
- 嘗試的authenticate_user,但如果驗證失敗不失敗的過濾器鏈。
- 創建響應負載時使用current_user(current_user將包含零或經過身份驗證的用戶)。
就拿這個代碼:
class Api::MyController < Api::ApiController
before_filter :authenticate_user!
def index
if current_user
# Create the result json using current_user
else
# Create the result json without user data
end
render json: result
end
end
只有通過認證的用戶將通過的authenticate_user!調用並獲取索引方法。 我想所有用戶到達那裏,無論是已驗證和未驗證。
不錯的主意!我認爲它會工作,但似乎沒有得到認證失敗時的救援塊(我添加了一個斷點,它從來沒有達到它).. – user233232
你可以嘗試關注[這篇文章](http:// codebeerstartups .com/2013/01/custom-redirect-after-login-fail-in-devise /),如果認證失敗,則重定向到'index_auth_failed',但是當需要認證時,所有控制器的動作都會重定向到這個動作,I不知道這是你想要的! – ex0ns