Database – What’s the best way to modularize a User schema so it’s generic

databasedatabase-designmongoschema

I have a database design question. Basically I want to be able to create a schema for a User model, then use this User model in other models that extend User but I want to design it in such a way that it's generic enough to be used in every application.

For example a Profile or Account model might extend User, and in both cases they will be different based on the web application you are designing but the core credentials of User should never be different across any web application.

What fields do you think should be in the User model?

I think the bare minimum to successfully handle authentication would be:

  • email (the login unique identifier)
  • salt (obviously!)
  • password (duh)
  • lostToken (a hash to verify lost password functionality)
  • role (member, admin, editor, etc.. IMO this list of roles would differ between sites however it's too important to the User model not to have here?)

Now we get into other interesting fields that are still very very useful:

  • createdAt (when the account was created)
  • ipAddress (track the ip when the account was created)
  • refererUrl (which site it came from)
  • lastLoggedIn (the last time the user logged in)
  • isOnline (is the user currently online)

And even more fields that are still pretty useful:

  • username (might not be used on every site)
  • number of consecutive logins (similar to the stack network)

I think anything else like social data (likes, votes, profile views), badges/achievements, the last time they updated their profile/account/whatever, and other info like their name belong in the per-site Profile model.

What do you think?

Edit:
I fully understand that this question is partly subjective but I do think there's definitely room for discussion.

Best Answer

My first instinct would be to reuse an existing user authorization mechanism, rather than writing one. Then I would skip the rest until I had firm requirements. Premature generalization is just as bad as premature optimization.

Related Topic