forked from phoenixframework/phoenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
976050a
commit 1b44973
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
priv/templates/phx.gen.auth/registration_controller_api.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
priv/templates/phx.gen.auth/registration_controller_api_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |