1

I am working on a rails app that allows users to log in via facebook/twitter/linkedin using omniauth. So far, users are able to sign up and create an account using the authentications, but they must pass validations and are therefore forwarded to a signup page where they must enter a valid name, username, and email. I want these fields to be already filled out if possible using the request.env["omniauth.auth"] hash.

Here is my code:

authentications_controller.rb

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

registrations_controller.rb:

def build_resource(*args)
  super
  if session[:omniauth]
    @user.apply_omniauth(session[:omniauth])
    @user.valid?
  end
 end

user.rb:

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

The line:

self.email = omniauth['user_info']['email'] if email.blank?

results in this NoMethodError:

undefined method `[]' for nil:NilClass

The session[:omniauth] is being passed from the registrations controller to omniauth in the apply_omniauth method. How do I access the name and email in this session?

Thanks

Sabar
  • 478
  • 2
  • 20
  • 1
    When they released omniauth 1.0, they changed the auth['user_info'] to just auth['info'] - A handy piece of info to know if you are upgrading gem versions... – Astra May 31 '12 at 19:15

1 Answers1

5
  1. Quick answer:

    omniauth.info.email # which is the same as omniauth['info']['email']
    
  2. Explanatory answer:

    Put this as the first line of your callback controller:

    render :text => "<pre>" + env["omniauth.auth"].to_yaml and return    
    

    Now try to login and you'll be able to take a good look at the hash of nested hashes returned.

Ashitaka
  • 19,028
  • 6
  • 54
  • 69
  • Thanks first ! For the explanatory answer, where should we add that line of code ? – CanCeylan Nov 12 '12 at 18:49
  • Surely you have a `SessionsController` or `AuthenticationsController` that receives the callback from facebook or twitter or what have you. Generally, the action that is called is the `create` action, right? All you have to do is put that line of code at the beginning of the `create` action. – Ashitaka Nov 12 '12 at 18:54
  • When I added that code in the beginning of the create method of AuthenticationsController, I had "RuntimeError in AuthenticationsController#create" :/ – CanCeylan Nov 12 '12 at 18:59
  • Yes, that's the point. The purpose of raise is to create an error. In this case, create an error with the contents of the request. – Ashitaka Nov 12 '12 at 19:03
  • I know it sound a little bit stupid but after deleting the raise thing and adding quick answer again I started to have "undefined method `serialize_into_session' for Symbol:Class". Is it sth related to that ? – CanCeylan Nov 12 '12 at 19:13
  • 1
    I'm pretty sure you are using an old tutorial. Follow this answer: http://stackoverflow.com/a/11097308/1160916 – Ashitaka Nov 12 '12 at 19:16
  • Yes, I was following the old one, but the thing is even though I switched back to old code, it doesn't work now. Is there anyways to rollback ? – CanCeylan Nov 12 '12 at 19:35