2011-06-24 79 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