Bedtimestory9

Bedtimestory9

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?

Showing Posts 1 to 5

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?

Bedtimestory9

Bedtimestory9 OP

Hi @benwilson512 sorry for the ambiguity, I’ll try my best to clarify.
My knowledge on Phoenix is as much as the first chapter on Phoenix Framework by Pragmatic studio

This is my registration process:

defmodule CompoCenter.Accounts.User do
...
  def changeset(user, attrs) do
    user
    |> cast(attrs, [:email, :company, :first_name, :last_name])
    |> validate_required([:email, :company, :first_name, :last_name])
    |> validate_format(:email, ~r/@/)
  end

  def registration_changeset(user, params) do
    user
    |> changeset(params)
    |> cast(params, [:password])
    |> validate_required([:password])
    |> validate_length(:password, min: 6, max: 100)
    |> put_pass_hash()
  end
...
end

So so far as long as the user fill out their email with an @ symbol as well as complying with password length rule, the user will be inserted into the Repo.

And I want to have it differently, I want to have an admin(or me) to go through a review of their input, once I click approve, then will it be created.

If this is still too vague, can you point me to what topics should I explore? I am pretty much in the fog too. But I’m sure there’re websites like this. For example, you submit a registration form to create an account on a website, and the website responded with “Thank you for your submission, your account will need to be approved, which might take a few days.” Or something like that.

Yeah I think my question contains too many topics, if you can give me some pointer to what should I read I’ll greatly appreciate it.

kokolegorille

kokolegorille

One solution is to have a status field on the User. For example approved, not_approved. Then You can ensure only approved user have access ?!

The admin just need to toggle this field to allow user access

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.

Bedtimestory9

Bedtimestory9 OP

This answers my question. Though the book didn’t use liveview from the start, I’m glad to know the implementation logic is almost the same on my Auth.ex.

I’m having a little trouble to add a timestamp to the approved_at field atm, as well as the nil type which is not recognized by phoenix but I’ll crunch through some more docs to get at it. Thanks for the improvement!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
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
JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews