“BCrypt::Errors::InvalidHash” when trying to sign in

后端 未结 2 1886
温柔的废话
温柔的废话 2020-12-02 20:20

I am trying to finish a project. I am working with user models. When I signup everything seems ok . But when I try to signin the same member I get this error.

We\'

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 20:35

    I already had live users, and likewise already saved unencrypted passwords into the database. Once I started using bcrypt, it was expecting an encrypted password, and when it didn't find it, it produced this error.

    Therefore, I added this rescue to catch the error and prompt the legacy users to reset their password:

    begin
        # your code that attempts to login the user
    rescue BCrypt::Errors::InvalidHash
      flash[:error] = 'We recently adjusted the way our passwords are stored. Please click the "forgot username or password?" link to re-establish your password. Thank you for your understanding!'
      redirect_to password_resets_url
    end 
    

    Hope this helps.

提交回复
热议问题