Search code examples
ruby-on-railsrubyfacebookkoala

Rails Koala - Getting a undefined method `oauth_token=' for #<User:


Apologies for the beginner question. I'm trying to use the Koala gem on my app. Currently, I have devise and omniauth (for facebook login). And they work great - I'm able to register users easily. What I want to do now though is utilize Koala to get access to Facebook data from my logged in Users. However, I'm getting an error "undefined method `oauth_token=' for #

User.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.username = auth.info.name     
      user.avatar = URI.parse(auth.info.image)
      user.oauth_token = auth.credentials.token #Added this after learning about Koala
    end
  end

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)
  end
end

Application_Controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_filter :set_body_class
  before_filter :set_current_user

  def set_current_user
    User.current_user = current_user
  end

  def set_body_class
    @body_class = "#{controller_name} #{action_name}"
  end

  protected

  def after_sign_in_path_for(resource)
    # request.env['omniauth.origin'] || stored_location_for(resource) || root_path
    items_path
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar])
  end
end

Schema.rb

create_table "users", force: :cascade do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at",                          null: false
  t.datetime "updated_at",                          null: false
  t.string   "avatar_file_name"
  t.string   "avatar_content_type"
  t.integer  "avatar_file_size"
  t.datetime "avatar_updated_at"
  t.string   "username"
  t.string   "provider"
  t.string   "uid"
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

Migration AddOmniauthToUsers

class AddOmniauthToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :provider, :string
    add_column :users, :uid, :string
  end
end

enter image description here


Solution

  • Try and call self.outh_token. outh_token belongs to a user and unless you pass a user as a parameter or call it on itself using "self", Rails doesn't know where outh_token belongs to.

      def facebook
        @facebook ||= Koala::Facebook::API.new(self.oauth_token)
      end
    

    or

      def facebook(user)
        @facebook ||= Koala::Facebook::API.new(user.oauth_token)
      end