angelmz
Phx.gen.auth and role based authentication
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’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 7 of 7 Posts!
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.