I've followed the railscasts video #235 and #236 to setup OmniAuth w/ Devise. I can get everything to work fine, my issues comes when I add the conditional statements in the authentications controller. I receive the following error:
NoMethodError in AuthenticationsController#facebook undefined method `to_key' for :user:Symbol It seems to be crapping out at:
sign_in_and_redirect(:user, authentication.user)
I've checked other answers here and none seems to have a solution. I found one related to Heroku, but I'm getting this message in development. Any help would be greatly appreciated.
authentications_controller.rb
def facebook
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
registrations_controller.rb
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
routes.rb
resources :authentications
get '/auth/:provider/callback' => 'authentications#facebook'
devise_for :users, :controllers => {:registrations => 'registrations', :omniauth_callbacks => 'authentications'}
user.rb
class User ActiveRecord::Base
has_many :authentications
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def apply_omniauth(omniauth)
self.email = omniauth.info.email if email.blank?
authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end
def password_required?
(authentications.empty? || !password.blank?) && super
end
end
Log
ActiveRecord::SchemaMigration Load (0.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
I, [2014-04-09T20:17:07.296969 #2248] INFO -- omniauth: (facebook) Request phase initiated.
I, [2014-04-09T20:17:07.353447 #2248] INFO -- omniauth: (facebook) Request phase initiated.
I, [2014-04-09T20:17:07.518464 #2248] INFO -- omniauth: (facebook) Callback phase initiated.
Processing by AuthenticationsController#facebook as HTML
Parameters: {"code"=>"AQC6I3SgbGI86ZBptFdBal5HIE1UHwCt7Zw5FVzGvWtApGhZfxjmXTo-AX3qJGOA_PWOeMK4i05K2yJL4_8bQLaTUjyvOBO4nQHjz2nLv5CTCMg2fPAsveUquO-UdVA8XajfUai9AQ8U8m4tfkwxysnq4n2bGBot2UCmFcfRGDB_b9uoIO8ELj80ltc3T0Vf2wj6XScl_R5m1o3CTUYqUzLQCukyjCb3Mz5_2_cWvHiCJLEiTMWjwccbrqNFhWgFCDNE0sLWZnESFNbdWUvFb_ug0NoXk4JevtQjv9XbW-FBiZ3FACn49grGlum1Z0I6qbI", "state"=>"758f521ee2e37251f404435d13e5f194fc7dab59cb412659"}
Authentication Load (2.2ms) SELECT "authentications".* FROM "authentications" WHERE "authentications"."provider" = 'facebook' AND "authentications"."uid" = '578621157' ORDER BY "authentications"."id" ASC LIMIT 1
User Load (7.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 3]]
Completed 500 Internal Server Error in 124ms
Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.3ms)
Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.2ms)
Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.9ms)
Rendered /Users/csmears/.rvm/gems/ruby-1.9.3-p545/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (35.6ms)