I have a simple solution I’ve made myself with the following objects:
- Account (has token field, that is returned when authenticating and used in API calls)
- Authentication (has auth_type/auth_id and reference to Account)
I have a separate Authentication model to be able to connect several ways of login (device UUID, email/password, twitter, facebook etc). But it seems that in all examples of Devise you use it on the User (Account) model.
Isn’t that less flexible? For example OmniAuth module stores provider and id on the User model, what happens if you want to be able to login from both Twitter and Facebook, there is only room for one provider?
Should I use Devise on my Account model or the Authentication model?
Have been recently working on a project where I was using Devise to keep user’s tokens for different services. A bit different case, but still your question got me thinking for a while.
I’d bind Devise to Account model anyway. Why? Let’s see.
Since my email is the only thing that can identify me as a user (and you refer to Account as the User) I would place it in
accountstable in pair with the password, so that I’m initially able do use basic email/password authentication. Also I’d keep API tokens inauthentications.As you’ve mentioned, OmniAuth module needs to store provider and id. If you want your user to be able to be connected with different services at the same time (and for some reason you do) then obviously you need to keep both provider-id pairs somewhere, otherwise one will simply be overwritten each time a single user authenticates. That leads us to the Authentication model which is already suitable for that and has a reference to Account.
So when looking for a provider-id pair you want to check
authenticationstable and notaccounts. If one is found, you simply return anaccountassociated with it. If not then you check if account containing such email exists. Create newauthenticationif the answer is yes, otherwise create one and then createauthenticationfor it.To be more specific:
That should give you what you need while keeping the flexibility you want.