用Rails,BCrypt,Mongoid和MongoLab获取的用户在部署中失败

时间:2015-05-29 03:58:42

标签: ruby-on-rails mongodb heroku mongoid mlab

我是第一次试图通过使用Rails和Heroku让MongoDB玩得很好的海报。现在,我收到的错误让我觉得在登录时比较用户密码(应该用Bcrypt掩盖)存在问题。渴望更高级的眼睛,或更好地了解如何解决这个问题。

在应用程序中,我发现并创建了这样的用户:

def create
 password_key = BCrypt::Password.create(params[:user][:password]) 
 params[:user][:password] = password_key 
 newUser = User.create(params[:user])

 flash[:message] = "Account created! Please log in:"

 redirect_to controller: "users", action: "login"
end

def login
end

def find
 username = params[:user][:username]
 @user = User.where( username: username )[0]

 if @user
  if BCrypt::Password.new(@user.password) == params[:user][:password] 
    session[:user_id] = @user.id 
    redirect_to controller: "application", action: "home"
  else
    flash[:message] = "Wrong password. Try again."
    redirect_to controller: "users", action: "login"
  end
 else
  flash[:message] = "We didn't recognize that username."
  redirect_to controller: "users", action: "login"
 end

end

这对我来说在当地开发中工作得很好。当我第一次部署到Heroku时,我收到了一个"出了问题。"在尝试创建新用户时立即出错,这表明我没有正确配置我的生产环境。

经过更多阅读,我决定使用MongoLab来托管我的生产数据库。我在this tutorial.

之后添加了我的heroku存储库

然后我设置了我的heroku env。将MONGOLAB_URI变量为相应的URI,并在 mongoid.yml 文件中包含以下内容:

production:
 sessions:
 default:
  uri: <%= ENV['MONGOLAB_URI'] %>
  options:
    consistency: :strong
    safe: true
    skip_version_check: true

现在,当我访问实时应用时,我尝试创建用户时不再出错!我按照我的预期重定向,并收到我的&#34;帐户创建!请登录:&#34; flash消息。

HOWEVEVER,当我尝试使用我刚保存到数据库的凭据登录时,我被重定向并且我的密码错误。再试一次。&#34; flash message fires - 这对我来说表明密码的存储方式或与BCrypt / Mongoid / MongoLab的组合进行比较存在某种问题。

请帮忙!关于如何将BCrypt与Mongoid一起使用,我有什么遗漏吗?这只发生在我的制作中,我不确定如何排除故障。

提前致谢。

0 个答案:

没有答案