Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise Omniauth "encrypted_password may not be NULL" for new user

I am using Devise with Omniauth to have users sign into my app with Facebook. I used the Railscast tutorials to get it up and running.

If a user is already a member of my site authenticating through facebook works fine. The problem comes in when authenticating a new user with facebook. When it goes to create a new user for my User model I get the "users.encrypted_password may not be NULL" error. I can't figure out how to pass over the password to the User model from Facebook information.

This is what I have:

authentations_controller.rb

class AuthenticationsController < ApplicationController
 def index
  @authentications = current_user.authentications if current_user
 end

def create
  omniauth = request.env["omniauth.auth"]
  authentication = Authentication.find_by_provider_and_uid(omniauth['provider'],   omniauth['uid'])
 if authentication
   flash[:notice] = "Signed in successfully."
   sign_in_and_redirect(:user, authentication.user)
 elsif current_user
   current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])
   flash[:notice] = "Authentication successful."
   redirect_to authentications_url
 else
   user = User.new
   user.apply_omniauth(omniauth)
   if user.save
     flash[:notice] = "Signed in successfully."
     sign_in_and_redirect(:user, user)
   else
     session[:omniauth] = omniauth.except('extra')
     redirect_to new_user_registration_url
   end
  end
end

user.rb

 def apply_omniauth(omniauth)
  self.email = omniauth['user_info']['email'] if email.blank?
  authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
 end


 def password_required?
   (authentications.empty? || !password.blank?) && super
 end

Any help would be great, thanks in advance!

like image 988
looloobs Avatar asked Jul 10 '11 14:07

looloobs


People also ask

How do I find omniauth?

Now look for the Omniauth section, it should be towards the bottom of the file, but you can just quickly CMD+F to find 'omniauth'. These lines are telling Devise to look through the credentials file and to retrieve our info. The code itself is pretty understandable.

How can I Make my users happier with OAuth?

However, you could make your users happier by allowing them to access your application without creating a new account. They could simply log in using their existing Facebook, Twitter, Amazon, or DigitalOcean accounts. In fact, you could support authentication with any popular OAuth service provider. OAuth support is provided by the OmniAuth gem.

How to add OAuth support for multiple OAuth providers?

If you have used more OAuth providers, you will need a separate method for each of them. The name of the method should match the name of the provider. For example, to add support for Facebook, your method will be defined using def facebook. Your application is now ready. Fire up your server again: Visit your home page.


1 Answers

Add :password => Devise.friendly_token[0,20] when creating a new user from facebook omniauth.

I believe Devise is expecting something in the password field to create a User. Since there is no password when doing facebook oauth (not on your app side at least), you just need to create a dummy password as show above.

See this for more info: https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

like image 86
Kevin Tsoi Avatar answered Sep 24 '22 01:09

Kevin Tsoi