Skip to content

Commit

Permalink
Copy registration controller
Browse files Browse the repository at this point in the history
  • Loading branch information
iaguirre88 committed Feb 1, 2024
1 parent 976050a commit 1b44973
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/mix/tasks/phx.gen.auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,20 @@ defmodule Mix.Tasks.Phx.Gen.Auth do

case Keyword.fetch(context.opts, :api) do
{:ok, true} ->
api_files = []
api_files = [
"registration_controller_api.ex": [
controller_pre,
"#{singular}_registration_controller.ex"
],
"registration_controller_api_test.exs": [
web_test_pre,
"controllers",
web_path,
"#{singular}_registration_controller_test.exs"
],
"registration_json.ex": [controller_pre, "#{singular}_registration_json.ex"],
]

remap_files(default_files ++ api_files)

_ ->
Expand Down
23 changes: 23 additions & 0 deletions priv/templates/phx.gen.auth/registration_controller_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>RegistrationController do
use <%= inspect context.web_module %>, :controller

alias <%= inspect context.module %>

action_fallback <%= inspect context.web_module %>.FallbackController

def create(conn, %{"<%= schema.singular %>" => <%= schema.singular %>_params}) do
with {:ok, <%= schema.singular %>} <- <%= inspect context.module %>.register_<%= schema.singular %>(<%= schema.singular %>_params) do
{:ok, _} =
<%= inspect context.alias %>.deliver_<%= schema.singular %>_confirmation_instructions(
<%= schema.singular %>,
&url(~p"<%= schema.api_route_prefix %>/confirm/#{&1}")
)

token = <%= inspect context.alias %>.create_<%= schema.singular %>_api_token(<%= schema.singular %>)

conn
|> put_status(:created)
|> render(:create, %{token: token})
end
end
end
29 changes: 29 additions & 0 deletions priv/templates/phx.gen.auth/registration_controller_api_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>RegistrationControllerTest do
use <%= inspect context.web_module %>.ConnCase<%= test_case_options %>

describe "POST <%= schema.api_route_prefix %>/register" do
test "creates account and logs the <%= schema.singular %> in when data is valid", %{conn: conn} do
conn =
post(conn, ~p"<%= schema.api_route_prefix %>/register",
<%= schema.singular %>: %{email: "[email protected]", password: "this_is_a_password"}
)

%{"access_token" => access_token, "token_type" => token_type} = json_response(conn, 201)

{:ok, <%= schema.singular %>} = <%= inspect context.module %>.fetch_<%= schema.singular %>_by_api_token(access_token)

assert token_type == "Bearer"
assert <%= schema.singular %>.email == "[email protected]"
end

test "returns error for invalid data", %{conn: conn} do
conn =
post(conn, ~p"<%= schema.api_route_prefix %>/register", <%= schema.singular %>: %{email: "with spaces", password: "too short"})

assert json_response(conn, 422)["errors"] == %{
"email" => ["must have the @ sign and no spaces"],
"password" => ["should be at least 12 character(s)"]
}
end
end
end
5 changes: 5 additions & 0 deletions priv/templates/phx.gen.auth/registration_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule <%= inspect context.web_module %>.<%= inspect Module.concat(schema.web_namespace, schema.alias) %>RegistrationJSON do
def create(%{token: token}) do
%{access_token: token, token_type: "Bearer"}
end
end

0 comments on commit 1b44973

Please sign in to comment.