user2109855
user2109855

Reputation:

Omniauth not working

So I've installed the Omniauth gem, and it's currently implemented with Devise.

Devise is functioning great however getting Omniauth setup has been a battle and it's something i'm still trying to get working. I am using Omniauth with Twitter authentication.

The problem: When I click on the 'Login with twitter' icon, it re-directs me to twitter as it should, I then am prompted to enter my twitter credntials..all is well until it begins to redirect (callback).

When it attempts to re-direct to my app i get the following error:

NoMethodError in AuthenticationsController#create

undefined method `[]' for nil:NilClass

app/models/user.rb:14:in `apply_omniauth'
app/controllers/authentications_controller.rb:14:in `create'

Registration controller:

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

Authentications controller:

class AuthenticationsController < ApplicationController
  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
end

User.rb (Users controller)

class User < ActiveRecord::Base
  has_many :authentications

  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable, :timeoutable and :activatable
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation
  # attr_accessible :title, :body

  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
end

Please read update:

I just ran into this following RailsCast.

The tutorial says to run:

rails g nifty:scaffold authentication user_id:integer \ provider:string uid:string index create destroy

But not having the nifty scaffold stuff on my machine, I just ran

rails g scaffold authentication user_id:integer \ provider:string uid:string index create destroy

Which behaves differently. Instead of creating stub 'index', 'create', and 'destroy' controller methods, it created fields in the database.

How can i remove the fields?

Upvotes: 1

Views: 856

Answers (2)

swapab
swapab

Reputation: 2410

In your User.rb line #14

instead of omniauth['user_info']['email'] it should be omniauth['info']['email']

For nifty generators you should have

gem "nifty-generators", group: :development

in you Gemfile

Try rails g scaffold --help to see the available options for scaffold generator

You may also have a look at this amazing rails-cast too generators-in-rails-3

Upvotes: 0

Abibullah Rahamathulah
Abibullah Rahamathulah

Reputation: 2891

Try Replacing

omniauth['user_info']['email']

by

omniauth['email']

you can use a wrapper method like this

Update: Wrapper for omniauth

def omni_conversion(omniauth)
    {
      # Required For Social Network Creation
      access_token: omniauth.credentials.token,
      link: omniauth.extra.raw_info.link,
      provider: omniauth.provider,
      providerid: omniauth.uid,

      # Required For User Creation
      birthday: omniauth.extra.raw_info.birthday,
      email: omniauth.info.email,
      first_name: omniauth.info.first_name,
      gender: omniauth.extra.raw_info.gender,
      last_name: omniauth.info.last_name,
      middle_name: omniauth.info.middle_name,
      picture: omniauth.info.image,
      timezone: omniauth.info.timezone,
      username: omniauth.extra.raw_info.username
     }
  end

omniauth = omni_conversion(omniauth)

The advantage is you get symbols to use directly. and you can pass them directly to models.

Upvotes: 1

Related Questions