Bedtimestory9
How to create an admin system that gets to approve user registration?
An admin will review all the info from a user’s register info and decide if the user will pass the approval or not. When new users submit their registration form, their info will send to our admin to have it approve. And only after approval could the new user be created.
So … How should I create such an admin system?
Most Liked
benwilson512
Hey @Bedtimestory9 your post has been moved to the moderation queue because there isn’t really enough information for someone to help you here. How much Elixir do you know? What sort of system is this? What have you tried so far, and where did you get stuck?
sodapopcan
Pretty much what the gorilla said.
A great resource to read is the output of mix phx.gen.auth. While it doesn’t provide this functionality out of the box, it’s really easy to tack on and the rest of the code is great study for how to properly implement authn.
Of course there are different ways to implement this with different trade offs but this probably what I would do. Firstly, for something like approval I prefer a dedicated field—approved_at as a timestamp—over a status so that what I’m going to use, but YMMV.
Assuming you’ve gen’d your auth with:
$ mix phx.gen.auth Accounts User users
Create a migration to add approved_at timestamp field to your users table, add the field to your User Schema (make sure it defaults to nil which means doing nothing).
In MyAppWeb.UserAuth look for def on_mount(:ensure_authenticated, .... This is where you will want to check if the user has been approved and deal with accordingly. You can change it to look something like this:
def on_mount(:ensure_authenticated, _params, session, socket) do
socket = mount_current_user(socket, session)
case socket.assigns.current_user do
nil ->
socket =
socket
|> Phoenix.LiveView.put_flash(:error, "You must log in to access this page.")
|> Phoenix.LiveView.redirect(to: ~p"/users/log_in")
{:halt, socket}
%{approved_at: nil} ->
socket =
socket
|> Phoenix.LiveView.put_flash(:error, "You have not been approved yet.")
|> Phoenix.LiveView.redirect(to: ~p"/")
{:halt, socket}
_ ->
{:cont, socket}
end
end
As far as notifying admins and making screens to manage the requests, I’ll leave that part up to you. It’s a simple LiveView although if you want Email notifications that is a whole other thing.
Popular in Questions
Other popular topics
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
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









