why

why

Hi,

I’m looking at the code phx.gen.auth injected in my project, trying to follow the logic to better understand how phx.gen.auth works.

While looking at the code, I bumped into a function Users.c/1 for which I cannot find a definition in the referenced Users module.

This is in the module MyProject.UserRegistrationController, in the create/2 function, in the case statement:

def create(conn, %{"user" => user_params}) do
    case Users.c(user_params) do
      {:ok, user} ->
        {:ok, _} =
          Users.deliver_user_confirmation_instructions(
            user,
            &Routes.user_confirmation_url(conn, :confirm, &1)
          )

        conn
        |> put_flash(:info, "User created successfully.")
        |> UserAuth.log_in_user(user)

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

Might anybody know where I can find the definition of Users.c/1 ?

Thank you.

First 3 of 3 Posts Switch mode

kokolegorille

kokolegorille

The code comes from priv/templates/phx.auth.gen/registration_controller.ex.

It should not look like what You have, the code is like this.

  def create(conn, %{"<%= schema.singular %>" => <%= schema.singular %>_params}) do
    case <%= inspect context.alias %>.register_<%= schema.singular %>(<%= schema.singular %>_params) do
      {:ok, <%= schema.singular %>} ->
        {:ok, _} =
          <%= inspect context.alias %>.deliver_<%= schema.singular %>_confirmation_instructions(
            <%= schema.singular %>,
            &Routes.<%= schema.route_helper %>_confirmation_url(conn, :confirm, &1)
          )

        conn
        |> put_flash(:info, "<%= schema.human_singular %> created successfully.")
        |> <%= inspect schema.alias %>Auth.log_in_<%= schema.singular %>(<%= schema.singular %>)

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

But I also see You have Users… You probably did not use the right parameters. It should be

mix phx.gen.auth Accounts User users

context plural (or not), schema singular, table plural.

APB9785

APB9785

Creator of ECSx

That function Users.c/1 should be Users.register_user/1 in lib/app_name/users.ex. Is it possible that at some point you made an accidental change to the file?

why

why OP

Thank you both for the help :slight_smile:

I just created a new Phoenix test project and ran the phx.gen.auth generator there.

This time I used mix phx.gen.auth UserAccounts User users (I cannot use Accounts, as I need that for something else).

Now the result looks good, with a call to UserAccounts.register_user/1:

def create(conn, %{"user" => user_params}) do
    case UserAccounts.register_user(user_params) do
      {:ok, user} ->
        {:ok, _} =
          UserAccounts.deliver_user_confirmation_instructions(
            user,
            &Routes.user_confirmation_url(conn, :confirm, &1)
          )

        conn
        |> put_flash(:info, "User created successfully.")
        |> UserAuth.log_in_user(user)

      {:error, %Ecto.Changeset{} = changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end
— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
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

We're in Beta

About us Mission Statement