Rails 4 Develop Omniauth with Multiple Providers Associated with a User Model

I have a question about how to approach integration design. I have two models Company and User . In my case, users will register for my application, and then the possibilities of connecting the application to their various accounts (Twitter, Facebook and LinkedIn) will be presented so that they can view / create messages / tweets / etc. Since Company has_many Users and social network accounts belong to the company, I want to associate the credentials with the Company model, using another model called Provider to store credentials, therefore allowing any user permission to access the companyโ€™s social network account.

I follow various tutorials on setting up several omniauth providers, but they all focus on binding them to the user model and tell me to add devise :omniauthable to my user model.

How to make Company omniauthable instead of user? Do I even need to make my model irreconcilable if I do not allow the user to check / register omniauth browsing?

This is my first time you are building integration into social networks, so I hope my question makes sense. Thanks!

+7
ruby-on-rails devise omniauth
source share
2 answers

After much thought, I came up with an approach that works very well and is much simpler than what I originally tried. This seems like a hack, so I'm certainly open to see if anyone has a cleaner approach.

The first thing I did was to refuse to make the company model indifferent, because I did not want users to register as a company, but rather as a company user. Instead, I created an omniauthable user model, and then created an Identity model in which the user and company belongs_to . User and Company as has_one Identity . As soon as the user permits the provider, a new identifier is created.

Since the user and the company have an identity, other users who belong_to can access this identity through the company. Similarly, if a user logs on to the system and does not have identifiers, the system checks if their company has any identifiers before allowing them to add them. Thus, for each company, there is only one identifier for each supplier, and it does not matter which user creates it.

0
source share

I have something similar to this job. We have an application that allows us to use omniauth and develop to identify OATH from FB, LI and Twitter. I keep Devise and Omniauth tied to the User model and have an Identity model that has a provider field, uuid and user_id relationship. In this way, using the email returned by the provider or the active session, I can determine if the user exists in the system and associates Identity accordingly. Twitter is a bit complicated because they do not return an email, so you need to add to determine if the user has a verified email, and if you do not need to request them for one. You can definitely do the same by making the company model omniauthable and devise_authentcatable, but it seems a bit controversial. I use a standard user approach and have a user who owns one or more companies. Hope this helps.

+2
source share

All Articles