Skip to main content

Subscribe to GoRails to get access to this episode and all other pro episodes, and new awesome content every month.

Subscribe Now
Only $19/month

Unlimited access. Cancel anytime.

14 Login with Multiple OAuth Providers using Omniauth

Episode 202 · August 15, 2017

Learn how to let your users login with multiple OAuth providers like Twitter, Facebook, Github, Google, etc all at the same time.

Authentication


Transcripts

Subscribe or login to view the transcript for this episode.

Discussion


Gravatar
Alex Musayev on

I never thought it is possible to authenticate with social media account before email validation.

I've been doing automated merging of social media accounts with the user profile in my app, if email addresses are matching. E.g. if a user signs up as [email protected], and then someone else signs in using Twitter OAuth provider, using Twitter account with the same email, Twitter identity will be merged with the application profile.

Now I need to fix the related use case.

Gravatar
Chris Oliver (169,620 XP) on

I think most services confirm your email, but I don't know for sure if they force you to do that before using OAuth. It's definitely something to think about. Automatic merging is probably okay so long as you can trust that those accounts were already confirmed. I'm just not 100% that they are. I would guess that every service is different so it's probably safest not to auto-merge. If you find out more details on that, let us know!


Gravatar
Josh Cooper (9,390 XP) on

Hi Chris Oliver,
I keep getting the following error "You cannot call create unless the parent is saved" on line 25 of the omniauth_callbacks_controller.rb

Was this working correctly with yours?

Gravatar
Dan Frenette on

So that error is a result of calling create on your service instance when the user it's associated to hasn't been saved to the database. I resolved this by creating by calling find_or_initalize_by on the user model, checking if that record was valid, and then calling save! on the record.

Gravatar
Lauro Silva on

Hey Dan! I'm having the same problem. The service instance are not being saved. How did you call find_or_initalize_by on the user model? I'm having a hard time implementing it.


Gravatar
Denis Dujota (410 XP) on

Hello Chris,

First off thank you kindly for this tutorial, it helped me a lot with my school project.

Now, down to business.

I have integrated the authentication and devise which all works like a charm.

So heres my new issue:: i want t make a post on my app and send it to multiple services and save it to my databse.

i have a Post model, which creates a post on my app and saves to database, but i want to take that post and broadcast it to twitter(and eventually facebook)

do i put the twitter::Rest::Client.new mehtod in the service model under the twitter method? seems like the likely candidate, but a little confused as goRails episode 33 uses it in the User model, which we have on this guide as the omniauth callback controller.

then i was thinking of using boolean logic to check if user.service.provider == twitter run the post_to_twitter method.

any feedback would be great :)


Gravatar
Nana Adjei Manu (80 XP) on
Hi Alex, I think twitter doesnt have the email from the auth_hash, so im getting an error . How do i get around this?
Gravatar
Nana Adjei Manu (80 XP) on
I have since been able to fix this issue, i had to manually request user email from my twitter application settings..

Login or create an account to join the conversation.