why
April 14, 2021, 2:55pm
1
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.
1 Like
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.
2 Likes
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?
2 Likes
why
April 15, 2021, 7:48am
4
Thank you both for the help
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
2 Likes