How to create user/admin roles in Phoenix?

Dear all,

i am building website with 3 user access

  1. for free user
  2. for paid user
  3. for admin user

i create 3 page,

  1. analytic page for free user
  2. ai page for paid user
  3. admin page for manage all user (free and paid user)

please give me insight to create the code, do i just use mix phx.gen.auth or others?

You might find the permit lib useful: Permit — permit v0.2.0

2 Likes

phx.gen.auth will generate some useful pieces, but not the whole thing. I recommend running it and reading the code it produces.

The role separation could be represented in a lot of different ways. It may even be better represented in multiple ways. For instance:

  • “admin” vs “non-admin” could be a flag on a shared User schema, or it could be two separate auth setups for Users and Admins.
    The latter can be useful in domains where making “a user cannot become an admin via hacking” difficult is important.

  • on the other hand, “free” vs “paid” is more likely to make sense as one schema, since the main purpose of free accounts is to convert into paid :stuck_out_tongue:

  • for paid users, there’s also the question of where to put payment info / plan details. I’d strongly consider keeping those separate from the User schema used for authentication, since customers love to ask “can I have one payment for my ten employees to all have accounts”. Even if you don’t allow for multiple users per account in this initial setup, it will be easier to change a has_one :user to has_many :users (versus splitting apart the functionality in a working app).

4 Likes

Thankyou Brother @slouchpie

Thankyou Brother @al2o3cr