我正在尝试部署我的rails应用程序。我在这里有一个小要求。
我没有使用Devise。我通过他的用户名和密码提供用户登录。但现在我也想通过他的电子邮件提供用户登录。
我该怎样才能做到这一点。它应该适用于用户名和电子邮件。
我的访问控制器登录操作如下:
def attempt_login
if params[:username].present? && params[:password].present?
p 'Fields Check'
found_user = User.where(:username => params[:username]).first
p "#{@found_user.inspect}"
if found_user
authorized_user = found_user.authenticate(params[:password])
end
end
p "#{authorized_user.inspect}"
if authorized_user
if authorized_user.email_confirmed
# mark user as logged in
session[:user_id] = authorized_user.id
session[:username] = authorized_user.username
redirect_to(:controller => 'users',:action => 'index')
else
flash[:error] = 'Please activate your account by following the
instructions in the account confirmation email you received to proceed'
redirect_to(:controller => 'home',:action => 'index')
end
else
p "Not a Registered User"
flash[:error] = "Invalid username/password combination."
redirect_to(:controller => 'home',:action => 'index')
end
end
提前致谢:)
答案 0 :(得分:1)
您只需更改初始检查,如下所示
if (params[:username].present? || params[:email].present?) && params[:password].present?
然后查询用户
User.where('username= ? OR email= ?', params[:username], params[:email]).first