I started watching Ryan Bates's tutorial on how to use Omniauth with devise (rails-cast 235 revised). I am having problems with the user.rb file. What he displays in his tutorial
devise :omniauthable, # ...
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.username = auth.info.nickname
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
isn't working for me, and it displays an ActiveModel::ForbiddenAttributesError with this line of code where(auth.slice(:provider, :uid)).first_or_create do |user|
highlighted. I am thinking his version doesn't work because I am using Rails 5. Anyways I've tried modifying the user.rb file. With the following code.
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.provider = auth.provider
user.uid = auth.uid
token = auth.credentials.token,
secret = auth.credentials.secret
end
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.twitter_data"]
# user.attributes = params
user.update(
email: params[:email],
password: Devise.friendly_token[0,20],
provider: data["provider"],
uid: data["token"],
token: data["credentials"]["token"],
secret: data["credentials"]["secret"]
)
end
end
end
def password_required?
super && provider.blank?
end
def update_with_password(params, *options)
if encrypted_password.blank?
update_attributes(params, *options)
else
super
end
end
end
I am able to get the twitter auth page to show up load for about a minute and then redirect back to my user sign up page without signing in and also without displaying any error messages. By the way does anybody know how to sign in with facebook using devise.
The problem is related to the attribute
auth
that you are passing toself.from_omniauth(auth)
. You should set abinding.pry
orputs auth
on your server log so see how this variable looks like..Probably when the
self.from_amniauth(auth)
method runs thisauth.slice()
it somehow run in errorAlso the
first_or_create
method may be creating a user and you may be missing something, triggering an error. So try to with the debug show use theuser.errors.full_messages
The idea hear is that the method is meant to find the
User
or create it. It is being called from the controller actionUsers::OmniauthCallbacksController#facebook
request
means this is the outgoingrequest
from your server tofacebook
ortwitter
..Facebook or Twitter will reply to this
request
with anAJAX response
, which will include the information you need to authenticate the user.Then with the below method
where(provider: auth.provider, uid: auth.uid).first_or_create
you will either create theuser
orfind
it from your db. The problem is you are probably writing this method wrong or calling it with the wrong parameters, or you did not set up theapi
with twitter to correctly work...from https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview