[英]Devise find by username or email
after setup devise i would like to get the user by username or email with in a api session安装设计后,我想在 api 会话中通过用户名或电子邮件获取用户
I passed login to parameters but rails spits out a error我将登录传递给参数,但 Rails 吐出错误
User.find_for_database_authentication(:login => params[:username][:email])
the overwrite of find_for_database_authentication on model is already done.模型上 find_for_database_authentication 的覆盖已经完成。
def self.find_for_database_authentication(warden_conditions)
Someone has any hint to spare?有人有任何提示吗?
Devise设计
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
I did this by overriding find_for_database_authentication
like this:我通过像这样覆盖
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]).first
end
end
Nothing more was needed.不需要更多了。
试试这个: User.find_by(username: 'username')
或User.find_by(email: 'your@email.com')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.