Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 Devise Omniauth with multiple providers tied to model associated with user

I have a question on how to approach the design of an integration. I have two models Company and User. In my case, users will register for my app and then be presented with the option to connect the app to their different accounts (Twitter, Facebook, and LinkedIn) so that they can view/create posts/tweets/etc. Because Company has_many Users and the social media accounts belong to the company, I want to tie the credentials to the Company model using another model called Provider to store the credentials, therefore allowing any user that has permission to access the company's social media account.

I've been following various tutorials on how to configure multiple omniauth providers, but they all focus on tying them to the user model and tell me to add devise :omniauthable to my user model.

How do I make Company omniauthable instead of the user? Do I even need to make my model omniauthable if I'm not going to allow user authentication/registration view omniauth?

This is my first time building social media integrations, so I hope my question makes sense. Thanks!

like image 320
Daniel Bonnell Avatar asked Sep 24 '15 19:09

Daniel Bonnell


1 Answers

I have something similar to this working. We have an app that allows uses omniauth and devise to allow oauth identification from FB, LI and Twitter. I keep the Devise and Omniauth tied to the User model and have an Identity model that has a provider field, a uuid and a user_id relation. This way using the email returned from the provider or the active session I can determine whether the user already exists in the system and link the Identity accordingly. With Twitter is a little tricky because they don't return an email so you have to add to determine whether the user has a verified email and if they don't you need to prompt them for one. You can definitely do the same thing by making the Company model omniauthable and devise_authentcatable but it seems a little counterintuitive. I use the standard user approach and have the user own one or more companies. Hope this helps.

like image 124
errata Avatar answered Oct 22 '22 03:10

errata