Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OmniAuth and Devise, how to set optional passwords

I am using OmniAuth and Devise to authenticate users. I would like users that have signed up using OmniAuth providers to be able to set an optional password (needed for API authentication) but I'm running into a wall.

If a user creates an account via OmniAuth and tries to set a password they get the following error:

BCrypt::Errors::InvalidHash in RegistrationsController#update

I believe this is because the password is blank. What's a good way around this? I've thought about generating a random password but the problem with that approach is the user needs to know the current password in order to edit settings.

Edit: I looked at allowing the user to change settings without requiring a current password and that's what I would like to do only if the user didn't have a password initially.

like image 557
David Avatar asked Feb 20 '11 13:02

David


2 Answers

An alternative is to add the following into your 'user' model class to bypass password verification if there is no password to verify, where provider is some field that is set when using external authentication.

def valid_password?(password)  
  !provider.nil? || super(password)  
end
like image 144
Andrew Smallbone Avatar answered Oct 06 '22 01:10

Andrew Smallbone


I assume you don't want the easy way out which would be to simply reset the password if they wanted to set it?

user.send_reset_password_instructions

like image 42
jtesch Avatar answered Oct 05 '22 23:10

jtesch