简体   繁体   English

如何注销使用自定义设计策略登录的用户

[英]How to logout user logged in with custom devise strategy

I need to logout a user that has been logged in my Rails app using custom Devise authentication strategy.我需要使用自定义设计身份验证策略注销已登录到我的 Rails 应用程序的用户。 This strategy is used in addition to database_authenticable strategy.除了 database_authenticable 策略之外,还使用此策略。 Is there a logic I need to add to my custom implementation of authenticate_user!是否需要将逻辑添加到我的身份验证用户的自定义实现中! method in custom strategy?自定义策略中的方法? Also do I need to implement destroy method in a custom sessions controller?我还需要在自定义会话控制器中实现销毁方法吗?

Currently my custom strategy logs in the user fine (hard coded for now) but I cannot logout the user as logout action calls my custom implementation of authenticate_user!目前我的自定义策略可以很好地登录用户(现在是硬编码),但我无法注销用户,因为注销操作调用了我对authenticate_user 的自定义实现! again and they are logged in again.再次,他们再次登录。

You can use skip_before_action callback in this case like in your controller put below code before all actions 在这种情况下,您可以使用skip_before_action回调,就像在所有操作之前放在代码下面的控制器一样

 skip_before_action :authenticate_user, only: [:your_logout_action]

This will skip authentication for your method and user won't log in again. 这将跳过您的方法的身份验证,用户将不再登录。

So far as I understood correctly, is that, let any action to logout the current_user in devise. 据我所知,正确的是,让任何行动在设计中logout current_user If it's so then it can be implemented like this 如果是这样的话就可以像这样实现

Let's say , 比方说,

class SomeController < ApplicationController
  before_action :authenticate_user!

  def some_action
    if some_logic == true #implement some_logic to return boolean(true/false)
      sign_out resource
      flash[:notice] = ''
      flash[:error] = 'You are logged out!'
      root_path
    end
  end
end

I was able to fix my issue. 我能够解决我的问题。 The issue was that I had hard coded valid? 问题是我的硬编码valid? method to return true all the time. 方法一直返回true。 When I added the logic to return true only if email and password params are present then the custom auth works as expected. 当我添加逻辑以仅在存在电子邮件和密码参数时返回true,则自定义身份验证按预期工作。

使用户手动注销。

sign_out(current_user) if current_user.present?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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