Bedtimestory9

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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

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.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement