Using google oauth for a project. Had it working, but wanted to pull the profile picture to display in my app. Once I was able to display the picture, I logged out and tried to relog back in with the oauth. It is now telling me that the name and email have already been taken. I am using a find_or_create_by in my User model to find or create by the oauth credentials. I don't know what is causing this as it was working just a bit ago. And now, when I go back to how my code first was, it is not working period. Just giving me the errors bc of validations(trying to create a record instead of finding the record in the database.)
sessions#create
def create
if params[:provider].present?
#user = User.find_or_create_from_auth(request.env['omniauth.auth'])
if #user
session[:user_id] = #user.id
flash[:notice] = "Logged in as #{#user.name}"
redirect_to dashboard_path
end
Users model-
def self.find_or_create_from_auth(auth)
find_or_create_by!(provider: auth.provider, uid: auth.uid, password_digest: 'N/A') do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.address = "123 ABC St"
user.password = 'n/a'
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save
end
end
Appreciate any help, just not seeing the disconnect here.
This line:
find_or_create_by!(provider: auth.provider, uid: auth.uid, password_digest: 'N/A') do |user|
You only are considering the uid, provider and password_digest. But as your error says the name and email have already been taken. Which means that at your users database you have a unique constraint in both. So even when the uid and provider are different, you can't persist a new instance at DB if it matches with another existing record.
So you can remove the unique constraint or add that validation to find_or_create_by
Related
Since two days i tried to make Login with Facebook on ruby on rails using devise but it returns me NULL as email's value. I read many articles on stackoverflow but a lot of them are from years ago and none have worked for me. Thanks you for helping.
Here is the message in my console when i choose my facebook account
def self.from_omniauth(auth)
unless auth.info.email
auth.info.email = "#{auth.info.name.split(' ').join}#facebook.com"
end
user = User.where("email= ?", auth.info.email).first
puts auth.info.picture
puts auth.info.name
puts auth.info.email
if user
return user
else
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
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
user.skip_confirmation!
user.uid = user.uid
user.provider = user.provider
end
end
In my rails 6 app, users have to sign in with twitter omniauth and devise. I have been able to implement it by following this tutorial.
The issue I have is that when a user revokes my app permissions from their twitter account, the omniauth access tokens and access secrets that I previously saved into my database becomes invalid. If the same user decides to re-authenticate, the user gets access to the app but the user's tokens are not updated in the database, rendering the existing tokens invalid.
My question is, how do I continuosly update the user columns in my database so I continue to receive valid access tokens from twitter, especially when the user has access tokens.
This is relevant code from my user model
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
user.username = auth.info.nickname
user.location = auth.info.location
user.access_token = auth.credentials.token
user.access_secret = auth.credentials.secret
end
end
My user controller looks like this
def twitter
#user = User.from_omniauth(request.env["omniauth.auth"])
if #user.persisted?
sign_in_and_redirect #user, event: :authentication #this will throw if #user is not activated
set_flash_message(:notice, :success, kind: "Twitter") if is_navigational_format?
else
session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra")
redirect_to new_user_registration_url
end
end
Find the user.
If no user - create user.
Update user (bothe exisitng and new) with fresh omniauth data
Something like this should do the job:
def self.from_omniauth(auth)
#find from omniauth
user = User.where(email: auth.email).first
#find or create
user ||= User.create(
email: data["email"],
password: Devise.friendly_token[0, 20]
)
#update with fresh data
user.name = auth.info.name
user.image = auth.info.image
user.expires = auth.credentials.expires
user.refresh_token = auth.credentials.refresh_token
end
I got it working with by using find_or_initialize_by
def self.from_omniauth(auth)
user = find_or_initialize_by(provider: auth.provider, uid: auth.uid)
user.email = auth.info.email
user.password = Devise.friendly_token[0, 20]
user.name = auth.info.name
user.username = auth.info.nickname
user.location = auth.info.location
user.access_token = auth.credentials.token
user.access_secret = auth.credentials.secret
user.save!
return user
end
Currently, login of facebook is going well, I can get profile picture.
However, only getting name is not an ordinary name, it is displayed as 4f427j700ef46f2555e6.
How to get it
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
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.avatar = auth.info.image # assuming the user model has an image
# If you are using confirmable and the provider(s) you use validate emails,
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
Display method
<h2><%= #user.name %></h2>
Why can not I get a name?
please tell me.
How can i get Facebook user email by omniauth for Rails?
This is my omniauth.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'] , ENV['FACEBOOK_SECRET'], :scope => 'email', :display => 'popup', :info_fields => 'name,email'
end
This is my model
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
puts "auth.providerauth.providerauth.providerauth.provider"
puts auth.info.inspect
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.username = auth.info.name.gsub(" ","") + new_token
user.email = auth.info.email
user.password = digest(new_token)
user.save!
end
end
But i only got this from the info
auth.providerauth.providerauth.providerauth.provider
#<OmniAuth::AuthHash::InfoHash image="http://graph.facebook.com/xxxx" name="xxx xxxx">
Seems like no email in return.
But i need the email to pass my model validation so what should i do?
Thanks!
in theory the user.auth.info.email should be the case
https://github.com/mkdynamic/omniauth-facebook
... but in reality Facebook don't necessary need to return email if user don't want to publish email. (for example Twitter will never give you response with email)
so the approach where you creating user directly from what Oauth callback returns may be wrong for you.
if you really rally need an email from user, try rather saving whatever the OAuth returns to an Identity model and then if email is present create User and if email is not present prompt the user to provide the email in "finish signup form" that will create the user.
Think about it: Technically speaking Oauth is a protocol where user can signup to your application without providing any credentials => the Provide-UID is what defines him not the email
So you either split the way how you handle session to User(email&password) or Identity(OAuth) so someting like current_user || current_identity in your controllers ....or you will link them when email is provided
It is present in auth.
auth.info.email
I'm using the omniauth-facebook gem in combination with Devise to enable users to login with their Facebook account. This works fine. The problem now lies in the creation of a new user, when first signing up with Facebook. Each User has one ExtendedProfile with detailed user info. So the email and password are columns in the users table, but location and profile_image are stored in the extended_profiles table.
When signing up for the first time, the following method is executed:
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]
end
end
Now this creates a record for the users table, but not an extended_profile. Any idea how I can create a record for extended_profiles with the right user_id and fill it with the information I get back from Facebook? Thanks very much in advance!
So I've found the solution while trying out house9's answer:
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.create_extended_profile(
:first_name => auth.info.first_name,
:last_name => auth.info.last_name,
:facebook_avatar => auth.info.image)
end
end
The user.create_extended_profile() line creates a relational record with the auth values filled in! :)
Have you tried setting it in the block? Not sure if this will work
def self.find_for_facebook_oauth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
# ....
user.extended_profile.build
end
end
another option is to put an after_create callback on the User to create the record
http://guides.rubyonrails.org/active_record_callbacks.html#available-callbacks
or you can conditionally check if the record exists, change up that code to:
def self.find_for_facebook_oauth(auth)
user = where(auth.slice(:provider, :uid)).first
unless user
user = User.new
user.provider = auth.provider
# ....
user.extended_profile.build
user.save
end
user
end