0

我有一个使用 Devise 进行用户身份验证的 Rails 应用程序。如果用户使用注册表单进行注册,但他碰巧使用了现有的有效用户名/密码,而不是给出错误,我只想登录用户。但我不知道该怎么做。

有什么建议么?

4

1 回答 1

0

您将需要覆盖设计 RegistrationsController 创建操作。在常规注册代码之后,您需要添加代码以检查用户是否已存在,并在电子邮件/密码组合匹配时登录。像这样的东西(基于您正在使用的设计版本 - 这是基于 2.2.4 的源代码)

class RegistrationsController < Devise::RegistrationsController
      def create
        build_resource

        if resource.save
          if resource.active_for_authentication?
            set_flash_message :notice, :signed_up if is_navigational_format?
            sign_up(resource_name, resource)
            respond_with resource, :location => after_sign_up_path_for(resource)
          else
            set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
            expire_session_data_after_sign_in!
            respond_with resource, :location => after_inactive_sign_up_path_for(resource)
          end
        else
          # Add code to check if user exists and sign in
          # I am just pasting in the sign_in code here and you will need to customize it as per your needs
          # You can directly use params and User model, or try an keep your code generic
          self.resource = warden.authenticate!(auth_options)
          set_flash_message(:notice, :signed_in) if is_navigational_format?
          sign_in(resource_name, resource)
          respond_with resource, :location => after_sign_in_path_for(resource)
        end
      end
end

并将控制器也添加到您的路线中:

devise_for :users, :controllers => { :registrations => "registrations"}

于 2013-10-11T09:02:03.373 回答