简体   繁体   中英

undefined method skip_confirmation! - devise, omniauth

Trying to set up facebook authentication using devise, omniauth (including facebook-omniauth) on an app hosted on heroku. Call to facebook API works, but I do not manage to skip the confirmation step after callback.

I followed the github tutorial on omniauth : https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

and also read and tried to implement this : Devise skip_confirmation! not working

But I keep getting the following error in my heroku log :

NoMethodError (undefined method `skip_confirmation!')

Here is how my devise.rb looks :

config.omniauth :facebook, "API_KEY", "API_SECRET"    

{:strategy_class => OmniAuth::Strategies::Facebook,
:scope => 'email, offline_access', :client_options => {:ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'}}}

Here is my omniauth_callbacks_controller.rb :

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
   # You need to implement the method below in your model (e.g. app/models/user.rb)
   @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

   if @user.persisted?
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
    set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
   else
    session["devise.facebook_data"] = request.env["omniauth.auth"]
    redirect_to new_user_registration_url
   end
  end
end 

Here is my user.rb model :

def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
 user = User.where(:provider => auth.provider, :uid => auth.uid).first
 unless user
       user = User.new(name:auth.extra.raw_info.name,
                    provider:auth.provider,
                    uid:auth.uid,
                    email:auth.info.email,
                    password:Devise.friendly_token[0,20],
                    )
       user.skip_confirmation!
       user.save
  end
  user
end

Thanks for your help !

So if I'm right (not 100% secure), you need to declare that your model has the module confirmable, add the confirmable module:

   devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable

And make sure that you have the fields for the confirmable module on your users table, you should have the fields confirmation_token and confirmed_at

If you don't have those fields, check on this answer how to add them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM