2011-06-24 77 views
0

我正在使用單個訪問令牌,並且要在退出時重置它。設計:在退出後重置令牌

一種可能的方法是調用reset_authentication_token!在模型中的after_token_authentication中,但是我必須知道這是模型中的sign_out操作。

如何以更好的方式實現這一目標? 我正在使用設計1.3.4

謝謝。

回答

0

只需在應用程序控制器中覆蓋「after_sign_out_path_for」,然後在其中重置令牌。不需要編寫自己的SessionController(儘管Nathan提到它非常簡單)。

下面是你應該使用方法:http://rubydoc.info/gems/devise/1.3.4/frames - (對象)after_sign_out_path_for(resource_or_scope)來使用會話控制器登出用戶

方法。您可以在ApplicationController中覆蓋它以爲自定義作用域提供自定義掛鉤。注意,與after_sign_in_path_不同的是,此方法接收帶有範圍的符號,而不是資源。

默認情況下是root_path。

歡呼聲

0

這是一個非常好的blog post,我用它來添加/重置令牌。

這當然只是一個簡單的例子,他在視圖中調用的鏈接是創建/銷燬令牌。但是,這些相同的路徑可以在您的控制器中使用,以達到相同的效果。

+0

謝謝,但它仍然不顯示如何重置標記登錄時。 – kambi

+0

是否有某些原因導致您無法使用reset_authentication_token!從你的控制器? –

+0

sign_out操作是位於Devise gem中的Devise :: SessionsController的一部分,它不想破解它。 – kambi

0

因爲我遇到同樣的問題。這是我發現:

Devise Wiki: simple token authentication example

Devise Wike: simple token authentication links

在我SessionsController <設計:: SessionsController我改寫destroy方法(從了代表色器件的一個改變吧)

def destroy 
    token_was_removed = remove_current_users_token_if_json_request 

    redirect_path = after_sign_out_path_for(resource_name) 
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)) 
    set_flash_message :notice, :signed_out if signed_out && is_navigational_format? 

    respond_with resource do |format| 
     format.html { redirect_to static_pages_login_path } 
     format.json { 
     if token_was_removed 
      render :status=>200, :json=>{:message => "Logout successful." } 
     else 
      render :status=>401, :json=>{:message => "Logout failed. Invalid token or some internal server error while saving." } 
     end 
     } 
    end 
    end 



    def remove_current_users_token_if_json_request 
    #remove the users authentication token if user is logged in 
    if current_user and request.format.json? 
     current_user.authentication_token = nil 
     return current_user.save 
    else 
     return false 
    end 
    end