0

Here's my model

class User < ActiveRecord::Base
  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.name = auth.info.name
      user.oauth_token = auth.credentials.token
      user.oauth_expires_at = Time.at(auth.credentials.expires_at)
      user.save!
    end
  end
end

Here's my controller

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

 def destroy
   session[:user_id] = nil
   redirect_to root_url
 end
 end

I tried looking into the database if any duplicate entry has been made. But no. Please let me know if you can help.

mlemboy
  • 387
  • 2
  • 3
  • 15
  • please add your server log. So that can explain your issues more specificly – Rubyrider Nov 24 '14 at 13:58
  • I overcame the problem. Now it says that user table does not exist. @Rubyrider – mlemboy Nov 24 '14 at 14:01
  • then somehow you made such operations or your database has been moved away. Please run migration or see if you need to run rake db:setup again. – Rubyrider Nov 24 '14 at 14:02
  • possible duplicate of [Rails 4.1.5 omniauth strong parameters](http://stackoverflow.com/questions/25399414/rails-4-1-5-omniauth-strong-parameters) – Fei Sep 10 '15 at 13:39

1 Answers1

0

Just change the where(condition) to:

where(provider: auth.provider, uid: auth.uid)

The 1st approach fails because of the method #permitted? which AR calls (if defined) to sanitize the attributes.

> h = auth.slice(:provider, :uid)
> h.class
=> OmniAuth::AuthHash < Hashie::Mash
> h.permmitted?
> false

However, a simple Hash will not have #permitted? defined, thus it will just continue:

> h = { provider: auth.provider, uid: auth.uid }
> h2.permitted?
NoMethodError: undefined method `permitted?' for {:provider=>"facebook", :uid=>"XXXX"}:Hash

Reference: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/forbidden_attributes_protection.rb#L19)

GregMoreno
  • 349
  • 2
  • 6