2

I am using Mongodb as database in rails and i got error when using /auth/linkedin/callback

NoMethodError in AuthenticationsController#create undefined method []' for nil:NilClass Rails.root: /home/prem/Music/heronhrm Application Trace | Framework Trace | Full Trace app/models/user.rb:57:in apply_omniauth' app/controllers/authentications_controller.rb:19:in `create'

Also when i remove self.email = omniauth['user_info']['email'] if email.blank? from usermodel then the validation errors arises in

users/sign_up Email can't be blank I want to implement for twitter,linkdin and facebook.

my authentication.rb

 class Authentication
  include Mongoid::Document

 belongs_to :user
    field :user_id, :type => String
    field :provider, :type => String
    field :uid, :type => String
 def self.find_by_provider_and_uid(provider, uid)
  where(provider: provider, uid: uid).first
end

end

my user model is like this

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

My authentications controller is like this

    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
  def destroy
    @authentication = current_user.authentications.find(params[:id])
    @authentication.destroy
    flash[:notice] = "Successfully destroyed authentication."
    redirect_to authentications_url
  end

protected

  # This is necessary since Rails 3.0.4
  # See https://github.com/intridea/omniauth/issues/185
  # and http://www.arailsdemo.com/posts/44
  def handle_unverified_request
    true
    end
    end

My registration controller is like this

class RegistrationsController < Devise::RegistrationsController
  def create
    super
    session[:omniauth] = nil unless @user.new_record?
  end

  private

  def build_resource(*args)
    super
    if session[:omniauth]
      @user.apply_omniauth(session[:omniauth])
      @user.valid?
    end
  end
end
regmiprem
  • 735
  • 2
  • 14
  • 41
  • Well, your omniauth is nil or omniauth['user_info'] is nil. Add `puts omniauth.inspect` at the beginning of your `apply_omniauth` function to see what is the issue. – Maxime Garcia Nov 29 '12 at 22:40

2 Answers2

4

Inside your app/models/authentication.rb add this

def self.find_by_provider_and_uid(provider, uid)
  where(provider: provider, uid: uid).first
end
  • Thanks it solve my previous problem but now i fetch this error NoMethodError in AuthenticationsController#create undefined method `[]' for nil:NilClass Rails.root: /home/prem/Music/heronhrm Application Trace | Framework Trace | Full Trace app/models/user.rb:57:in `apply_omniauth' app/controllers/authentications_controller.rb:19:in `create' – regmiprem Nov 27 '12 at 12:13
  • ya i did but same error occurs.. i already added this in my authentication controller also – regmiprem Nov 27 '12 at 12:19
  • 1
    You should not put it in authentications_controller.rb , but in application_controller.rb – Aggelos Avgerinos Nov 27 '12 at 12:21
  • Can you supply us with your registrations controller please? – Aggelos Avgerinos Nov 27 '12 at 12:30
  • soory for delay. i have net problem.. I edit my question . U can look. I did look your question http://stackoverflow.com/questions/13576185/linked-in-authentication-and-aggregate-data I am also making authentication for linkdin. I am very happy if u guide me. also how to fetch first name of user from linkdin and make it current user in devise. – regmiprem Nov 28 '12 at 04:26
  • Also when i remove self.email = omniauth['user_info']['email'] if email.blank? from usermodel then the validation errors arises in users/sign_up Email can't be blank Email can't be blank Encrypted password can't be blank Name can't be blank – regmiprem Nov 28 '12 at 04:34
  • I need your removing session[:omniauth] = nil unless @user.new_record? from your registrations controller should do the job. – Aggelos Avgerinos Nov 28 '12 at 11:42
  • same error occurs NoMethodError in AuthenticationsController#create undefined method `[]' for nil:NilClass – regmiprem Nov 28 '12 at 12:02
  • 1
    https://gist.github.com/2634b6ed5cbfa3bec165 This is my apply_omniauth method. I cannot see what's wrong with yours, hope this helps. – Aggelos Avgerinos Nov 28 '12 at 12:17
  • NoMethodError in AuthenticationsController#create undefined method `oauth_token_linkedin=' for # Rails.root: /home/prem/Music/heronhrm Application Trace | Framework Trace | Full Trace app/models/user.rb:73:in `apply_omniauth' app/controllers/authentications_controller.rb:19:in `create' I get this error now. if u have sample project using omniauth then it will help me. Thanks – regmiprem Nov 29 '12 at 04:12
  • Sorry can't share more of this project, it's closed source (most of it). The error is because you have not set the attribute oauth_token_linkedin for user – Aggelos Avgerinos Nov 29 '12 at 15:02
  • rails g migration add_oauth_token_linkedin_to_users and inside that migration write def change add_column :users, :oauth_linkedin_token, :string end – Aggelos Avgerinos Nov 29 '12 at 15:03
0

Did you add this in your model? If not added then add this and then try

key :provider, String
key :uid, String
Dipak Panchal
  • 5,996
  • 4
  • 32
  • 68