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?