diff --git a/lib/chess/auth/user.ex b/lib/chess/auth/user.ex index 7927536..efe8e66 100644 --- a/lib/chess/auth/user.ex +++ b/lib/chess/auth/user.ex @@ -3,7 +3,6 @@ defmodule Chess.Auth.User do use Ecto.Schema import Ecto.Changeset - alias Chess.Auth.User alias Comeonin.Argon2 schema "users" do @@ -14,15 +13,17 @@ defmodule Chess.Auth.User do timestamps() end - def changeset(user) do - user + def changeset(struct) do + struct |> cast(%{}, [:username, :password]) + |> validate_required([:username, :password]) + |> hash_password() end @doc false - def changeset(%User{} = user, attrs) do - user - |> cast(attrs, [:username, :password]) + def changeset(struct, params) do + struct + |> cast(params, [:username, :password]) |> validate_required([:username, :password]) |> hash_password() end diff --git a/lib/chess_web/controllers/page_controller.ex b/lib/chess_web/controllers/page_controller.ex new file mode 100644 index 0000000..bb7036d --- /dev/null +++ b/lib/chess_web/controllers/page_controller.ex @@ -0,0 +1,9 @@ +defmodule ChessWeb.PageController do + @moduledoc false + + use ChessWeb, :controller + + def index(conn, _params) do + render(conn, "index.html") + end +end diff --git a/lib/chess_web/controllers/registration_controller.ex b/lib/chess_web/controllers/registration_controller.ex new file mode 100644 index 0000000..14c402a --- /dev/null +++ b/lib/chess_web/controllers/registration_controller.ex @@ -0,0 +1,23 @@ +defmodule ChessWeb.RegistrationController do + use ChessWeb, :controller + + alias Chess.Auth.User + + def new(conn, _params) do + changeset = User.changeset(%User{}) + render(conn, "new.html", changeset: changeset) + end + + def create(conn, %{"user" => user}) do + changeset = User.changeset(%User{}, user) + + case Repo.insert(changeset) do + {:ok, _user} -> + conn + |> put_flash(:info, "Registered successfully.") + |> redirect(to: page_path(conn, :index)) + {:error, changeset} -> + render(conn, "new.html", changeset: changeset) + end + end +end diff --git a/lib/chess_web/router.ex b/lib/chess_web/router.ex index ce0ba67..614844f 100644 --- a/lib/chess_web/router.ex +++ b/lib/chess_web/router.ex @@ -16,9 +16,10 @@ defmodule ChessWeb.Router do scope "/", ChessWeb do pipe_through :browser # Use the default browser stack - get "/", GameController, :index + get "/", PageController, :index resources "/games", GameController, only: [:index, :create, :show, :delete] resources "/session", SessionController, only: [:new, :create], singleton: true + resources "/registration", RegistrationController, only: [:new, :create], singleton: true end # Other scopes may use custom stacks. diff --git a/lib/chess_web/templates/layout/app.html.eex b/lib/chess_web/templates/layout/app.html.eex index 4ea175d..35916c7 100644 --- a/lib/chess_web/templates/layout/app.html.eex +++ b/lib/chess_web/templates/layout/app.html.eex @@ -14,6 +14,9 @@
+

Chess

diff --git a/lib/chess_web/templates/page/index.html.eex b/lib/chess_web/templates/page/index.html.eex new file mode 100644 index 0000000..2a2d35c --- /dev/null +++ b/lib/chess_web/templates/page/index.html.eex @@ -0,0 +1,3 @@ +

Welcome

+ +

Sign in or register to get started.

diff --git a/lib/chess_web/templates/registration/new.html.eex b/lib/chess_web/templates/registration/new.html.eex new file mode 100644 index 0000000..f0e9ffd --- /dev/null +++ b/lib/chess_web/templates/registration/new.html.eex @@ -0,0 +1,21 @@ +

Register

+ +<%= form_for @changeset, registration_path(@conn, :create), [class: "create-registration"], fn f -> %> + <%= if @changeset.action do %> +
+

Oops, something went wrong! Please check the errors below.

+
+ <% end %> + +
+
+ <%= input f, :username %> +
+
+ <%= input f, :password, as: :password %> +
+
+
+ <%= submit "Register", class: "btn btn-primary" %> +
+<% end %> diff --git a/lib/chess_web/views/page_view.ex b/lib/chess_web/views/page_view.ex new file mode 100644 index 0000000..d2b928b --- /dev/null +++ b/lib/chess_web/views/page_view.ex @@ -0,0 +1,3 @@ +defmodule ChessWeb.PageView do + use ChessWeb, :view +end diff --git a/lib/chess_web/views/registration_view.ex b/lib/chess_web/views/registration_view.ex new file mode 100644 index 0000000..a222b01 --- /dev/null +++ b/lib/chess_web/views/registration_view.ex @@ -0,0 +1,3 @@ +defmodule ChessWeb.RegistrationView do + use ChessWeb, :view +end diff --git a/test/features/registration_test.exs b/test/features/registration_test.exs new file mode 100644 index 0000000..c50fe6b --- /dev/null +++ b/test/features/registration_test.exs @@ -0,0 +1,16 @@ +defmodule Chess.RegistrationTest do + use ChessWeb.FeatureCase + + import Wallaby.Query, only: [text_field: 1, link: 1, button: 1] + + test "user can register", %{session: session} do + session + |> visit("/") + |> click(link("Register")) + |> fill_in(text_field("Username"), with: "link@example.com") + |> fill_in(text_field("Password"), with: "ilovezelda") + |> click(button("Register")) + + assert session |> has_text?("Registered successfully") + end +end