ruby 通过用户名或电子邮件查找

v64noz0r  于 12个月前  发布在  Ruby
关注(0)|答案(2)|浏览(94)

安装后,我想通过用户名或电子邮件获取用户在API会话
我将login传递给参数,但rails显示错误

User.find_for_database_authentication(:login => params[:username][:email])

在模型上覆盖find_for_database_authentication已经完成。

def self.find_for_database_authentication(warden_conditions)

有人能给点提示吗?

设计

class Api::SessionsController < Devise::SessionsController

  respond_to :json

  def create
    resource = User.find_for_database_authentication(:login => params[:username][:email])


    if resource.valid_password?(params[:login][:password])
      sign_in(:user, resource)

      render :json=> {:auth_token=>resource.authentication_token, :email=>resource.email}, :status => :ok
      return
    end
    invalid_login_attempt
  end
end
k4ymrczo

k4ymrczo1#

我通过像这样重写find_for_database_authentication来做到这一点:

class User
  def self.find_for_database_authentication(auth_hash)
    self.where("username = :query OR email = :query", query: auth_hash[:email].downcase).first
  end
end

不需要更多了。

a64a0gku

a64a0gku2#

试试这个:User.find_by(username: 'username')User.find_by(email: ' [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) ')

相关问题