1
votes

I have following models (models don't overlap, an accountant can not be a customer):

  • Company has many accountants
  • Accountant has many customers

Accountants and customer can sign in / up.

What is the best way to do the auth?

Option A:

Use Devise with two separate models (Accountant & Customer: https://github.com/plataformatec/devise/wiki/How-to-Setup-Multiple-Devise-User-Models

Option B: Use Devise with one Model user, then another model Profile, which has some information, for example, column role.

Pros and cons would be very helpful, since I ask my question myself every time I build such a Rails App. I am used to work with Option B

Thanks.

1

1 Answers

2
votes

Well, the right solution depends on how you are going to handle these 2 types of users, accountants and customers. Both options are reasonable, however, I would stay with Option B.

Pros are:

  • You don't have to worry about the same logins of Accountants and Customers models. When using the same Sign In form for logging in customers and accountants (I doubt that there will be 2 different login form for them) the controller will have to find out if it is a customer or accountant. There can be ambiguity if the given login will be in customers and accountants table. When you use only one table, users, there is no such problem.
  • You don't have to duplicate code related to authorization. Sooner or later, but with 2 different models, it's inevitable. Different views, different mail templates, sometimes different controllers.
  • I'm not sure about your case, but the same user can be a customer AND accountant at the same time. It's more about usability, and some applications really separate these accounts: that double-role users have separate logins, one for accountant role and other for customer. As for me, it's not user-friendly, and it's better to have a single login with an option to switch between the roles when logged in.

On the other hand, if accountants and customers do not overlap at all, like regular users and admins, then it makes sense to have them in different models. But in this case they would need different interfaces too, the same as admin panel for site administrators and regular UI for regular users.