【问题标题】:How to authenticate using warden.authenticate!如何使用warden.authenticate 进行身份验证!
【发布时间】:2010-12-30 01:55:08
【问题描述】:

我想在我的论坛发表评论时即时进行“登录或注册”。我正在使用 Devise/Warden。

我想做这样的事情:

user = warden.authenticate!(params[:email], params[:password])
sign_in(:user, user)

谁能给我一些关于如何做到这一点的提示?

谢谢!

【问题讨论】:

    标签: ruby-on-rails devise warden


    【解决方案1】:

    Warden 有一个 set_user 辅助方法。所以你应该能够做到:

    warden.set_user(@user, scope: :user)

    您可能可以定义一个控制器辅助方法,以登录用户并将他们重定向到您想要的路径

    更多信息请访问:https://github.com/hassox/warden/wiki/Authenticated-session-data

    【讨论】:

      【解决方案2】:

      在您的模型中打开 :registerable 但不是 :confirmable。

      【讨论】:

        【解决方案3】:

        我知道这是一个老话题,但我已经使用它了

        # Inside SessionController < Devise::SessionsController
        def create
            self.resource = warden.authenticate!(auth_options)
            sign_in(resource_name, resource)  # this will set cookie
        end
        

        它是特定于设计的,但是在设计控制器中检查 auth_options 方法时,我发现了这段代码 here:

        # File 'app/controllers/devise/sessions_controller.rb', line 45
        
        def auth_options
            { scope: resource_name, recall: "#{controller_path}#new" }
        end
        

        希望有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-24
          • 1970-01-01
          • 1970-01-01
          • 2017-11-10
          • 2011-07-15
          • 2020-05-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多