I am trying to integrate omniauth-facebook into my web site but I keep getting the following err (or something similar) on callback:
NoMethodError (undefined method name=' for #<User:0x007f507c6f9158>):
app/models/user.rb:14:inblock in find_for_facebook_oauth'
app/models/user.rb:9:in find_for_facebook_oauth'
app/controllers/users/omniauth_callbacks_controller.rb:4:infacebook'
Here is my user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable, :omniauth_providers => [:facebook]
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name
user.image = auth.info.image
end
end
end
Not sure why it is complaining about procedure "name" - shouldn't it be looking for a field ? Any help appreciated.
You are getting undefined method name=' for #<User:0x007f507c6f9158> error on
user.name = auth.info.name
It just means that you don't have a field named name in users table. So, the setter method name= (Note = at the end of method name) does not exist in the User model.
If you are planning to use name field on users then add it in the users table.
Create a migration file for adding name field in users table by following command
rails generate migration AddNameToUsers name:string
Run rake db:migrate
Related
I'm trying to get GitHub users' location using Omniauth. Everything works fine except the location (I get nil even if mine is set to "Paris"). The documentation says it should be "info.location" to retrieve it. Here is my code in my Rails app:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.nickname = auth.info.nickname
user.name = auth.info.name
user.location = auth.info.location
end
end
end
Is it possible to retrieve GitHub Users' location?
I am trying to implement reddit login in rails application using omniauth-reddit
login is succesfull but i don't no where its going wrong
My OmniauthCallbacksController.rb file
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def reddit_oauth2
#user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect root_path
end
My User Model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:reddit]
def self.from_omniauth(auth)
where(provider: auth.provider,uid: auth.id).first_or_create do |user|
user.provider = auth.provider
user.id = auth.id
user.password = Devise.friendly_token[0,20]
end
end
end
can anybody suggest me scope or solution ?? Thanks in Advance
After spending 30+ hours trying to get a solution to this and searching all corners of the earth, I am posting my first question here ever.
No matter what I try, I get
'The parameter app_id is required'
I've followed the docs to a T from https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
Here are the pieces I thought were most important...any ideas what I could be missing?
config/initializers/devise.rb
config.omniauth :facebook, ENV["FACEBOOK_APP_ID"], ENV["FACEBOOK_APP_SECRET"], scope: 'user'
I've checked that the env params are correct on my system.
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
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.name = auth.info.name # assuming the user model has a name
# user.image = auth.info.image # assuming the user model has an image
end
end
end
Try this instead :
config.omniauth :facebook, "xxxxx", "xxxx"
Using the actual values inside config/initializers/devise.rb worked for me.
I am in the process of trying to integrate Omniauth Facebook login with Devise for a Rails application that I'm currently in the early stages of developing. I'm a (very) junior developer, and I've hit a roadblock which I'm unsure how to resolve.
So far, I have managed to install the Devise gem successfully, and have managed to get the Omniauth Facebook working up to the point of logging into the app with Facebook (by following the documentation at https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview) - however, on the redirect back to my app, the following error message displays:
NoMethodError in Users::OmniauthCallbacksController#facebook
undefined method `name=' for #
user.name = auth.info.name # assuming the user model has a name
The code snippets that I think would be helpful to resolve the issue are as follows:
My user model:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, :omniauth_providers => [:facebook]
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
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.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
end
end
end
My routes file:
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
root to: "restaurants#index"
devise_scope :user do
get 'sign_out', :to => 'devise/sessions#destroy', only: :destroy_user_session
end
resources :restaurants do
resources :reviews
end
end
I hope that this is enough to help me diagnose the problem, though please do let me know if any further code is required. Huge thanks in advance!
Does your user model have a name and image? Try removing these two lines:
user.name = auth.info.name # assuming the user model has a name
user.image = auth.info.image # assuming the user model has an image
I followed this tutorial precisely but I am getting this error:
NoMethodError in SessionsController#create
undefined method `slice' for nil:NilClass
Rails.root: /Users/raybesiga/Documents/Sites/foodie
Application Trace | Framework Trace | Full Trace
app/models/user.rb:29:in from_omniauth'
app/controllers/sessions_controller.rb:3:increate'
However my user.rb file is as below:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :provider, :uid
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
# attr_accessible :title, :body
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
My sessions_controller.rb file is as follows:
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["ominauth.auth"])
# session[:user_id] = user.user_id
session[:user_id] = user.id
redirect_to root_url
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
end
Any idea why I get this error?
You spelled env['omniauth.auth'] wrong in your sessions controller. You put env['ominauth.auth']