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.