why

why

Where is Users.c() function defined in phx.gen.auth?

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.

Most Liked

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

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

Where Next?

Popular in Discussions Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
lorenzo
Hey everone! I created a prototype for my app using Nodejs for the api. But the framework I chose wasnt great (in general theresnt any g...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
arcanemachine
https://nitter.net/josevalim/status/1744395345872683471 https://twitter.com/josevalim/status/1744395345872683471
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement