angelmz
Is phx.gen.auth good enough for role based authentication?
Let’s say for example one was building an e-commerce store.
There would standard users, admins, and sellers.
Could one do:
mix phx.gen.auth Accounts User users
mix phx.gen.auth Accounts Admin admins
mix phx.gen.auth Accounts Sellers sellers
I know that would create a lot of duplicate code, but if you got in there and specified which functions were fore each role, would this work? Also, could this be adaptable to phx.gen.auth? I couldn’t find anything regarding roles in the documentation.
Second alternative: Could one have and admins table that inherits the users table? I’m fairly new to Ecto and Postgres and I don’t know if that works here.
Thank you for your time
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tomkonidas
I would do it once with
mix phx.gen.auth Accounts User usersand then update the migration to have aroleenum field (could be[:customer, :admin, :seller]). That is the good thing about get.auth, it lets you modify the code after the facttadasajon
I’ve spent some time building a Phoenix app with auth and if I had to do it all over again I would use Supabase.com because they not only give you auth, but they also give you a very slick Postgres layer. You can still use Elixir/Phoenix to connect to it. Using Supabase for data+auth and connecting to it with Phoenix seems to me to be the most flexible approach all around. Auth is such a pain-in-the-neck when you don’t basically outsource it.
al2o3cr
It’s hard to say whether this is the “right” approach - it’s an approach, but whether it’s right depends on what the application does with users.
For instance, if all three kinds of users do the same sorts of things - or even more, if they all end up attached to the same records - then keeping them in three tables will be a headache.
On the flip side, if they do very different things this approach could be valuable since it’s possible to enforce things like “this can only point to a seller”. There may even be security benefits in some situations, since a User cannot become an Admin by overwriting columns in the database.
Another alternative approach: make a model that manages just the email + password part (call it something like
Credential) withphx.gen.auth, then have acredential_idon Admin / User / Seller etc. This could help reduce the duplication of things like password reset machinery etc (IF that machinery is supposed to be the same!)dub2
hi can you give a rough guide on how to go about setting up a hello world phoenix project with supabase taking care of auth?
anil28
this library seems to be interesting for authorization needs along with phx_gen_auth
let_me
Sanjibukai
I know that auth is something to take seriously…
So, rolling out your own auth system is out of the question…
One solution is going with a library (which I dislike because of some magic and things being implicit and hidden) so I really liked
phx_gen_authbecause how everything is transparent and explicit.Another solution is going with external tools like keycloak.
I never looked at supabase (or firebase) thinking about it being a database layer for frontends but with auth facilities.
So your comment is quite interesting and I’m also interested to read more about the subject of using supabase for the auth layer (if you have any links it’ll much appreciated
derpycoder
This is the best resource to understand and implement Authorization flow:
https://www.leanpanda.com/blog/authentication-and-authorisation-in-phoenix-liveview/#step-4-create-a-way-to-logout-a-user-and-close-all-his-her-sessions
I took it to next level with FunWithFlags feature toggles!!
Here are the reason:
Role based: falls short, as it suffers from Role Explosion.
Claim/Group Based: falls short, as anyone can claim to be something they are not! (e.g. Underage user claiming to be able to drink!!)
Policy Based: falls short, because once you define a policy that someone must be above 18 yrs old to claim to be able to drink, you won’t be able to change that at run time!!
Permission Based, it’s a combination of all of the above, so I have all the flexibility I could think of, and I can grant or deny access at runtime!! (It falls short because the decision is boolean, i.e. True or False, but world isn’t black or white)
Scope Based: Later I will add scopes, so I can limit access even further on top of Permissions. (For instance, an Admin shouldn’t be able to view Private user posts, or One Tenant can’t change data of another Tenant, etc)
For example:
Or maybe I will create a web page, so I can control the scope dynamically.
See how to dynamically write business logic on web; opus, exop, espresso:
For more info on RBAC vs CBAC, see:
Here’s how it looks right now:
lib/derpy_coder/photos.ex
lib/derpy_coder/photos/policy.ex
Usage in HEEX:
In EX Side:
Similary for other resources:
Here’s how to grant or deny Permissions:
Seeds.exs
Here’s how I enable or disable access in LiveBook.
Later I will have a web view for toggling feature, permission and any other types of flags. (I wanted unified UI instead of using UI that comes with Fun With Flags!!)
P.S. It made the testing difficult, will try to figure out testing after Phoenix 1.7 is out.