mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Registration works
This commit is contained in:
parent
c080985fc4
commit
558b4a5ff9
@ -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
|
||||
|
||||
9
lib/chess_web/controllers/page_controller.ex
Normal file
9
lib/chess_web/controllers/page_controller.ex
Normal file
@ -0,0 +1,9 @@
|
||||
defmodule ChessWeb.PageController do
|
||||
@moduledoc false
|
||||
|
||||
use ChessWeb, :controller
|
||||
|
||||
def index(conn, _params) do
|
||||
render(conn, "index.html")
|
||||
end
|
||||
end
|
||||
23
lib/chess_web/controllers/registration_controller.ex
Normal file
23
lib/chess_web/controllers/registration_controller.ex
Normal file
@ -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
|
||||
@ -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.
|
||||
|
||||
@ -14,6 +14,9 @@
|
||||
<body>
|
||||
<div class="container">
|
||||
<header role="banner">
|
||||
<nav role="user">
|
||||
<%= link("Register", to: registration_path(@conn, :new)) %>
|
||||
</nav>
|
||||
<h1>Chess</h1>
|
||||
</header>
|
||||
|
||||
|
||||
3
lib/chess_web/templates/page/index.html.eex
Normal file
3
lib/chess_web/templates/page/index.html.eex
Normal file
@ -0,0 +1,3 @@
|
||||
<h2>Welcome</h2>
|
||||
|
||||
<p>Sign in or register to get started.</p>
|
||||
21
lib/chess_web/templates/registration/new.html.eex
Normal file
21
lib/chess_web/templates/registration/new.html.eex
Normal file
@ -0,0 +1,21 @@
|
||||
<h2>Register</h2>
|
||||
|
||||
<%= form_for @changeset, registration_path(@conn, :create), [class: "create-registration"], fn f -> %>
|
||||
<%= if @changeset.action do %>
|
||||
<div class="alert alert-danger">
|
||||
<p>Oops, something went wrong! Please check the errors below.</p>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="form-group">
|
||||
<div class="form-field">
|
||||
<%= input f, :username %>
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<%= input f, :password, as: :password %>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<%= submit "Register", class: "btn btn-primary" %>
|
||||
</div>
|
||||
<% end %>
|
||||
3
lib/chess_web/views/page_view.ex
Normal file
3
lib/chess_web/views/page_view.ex
Normal file
@ -0,0 +1,3 @@
|
||||
defmodule ChessWeb.PageView do
|
||||
use ChessWeb, :view
|
||||
end
|
||||
3
lib/chess_web/views/registration_view.ex
Normal file
3
lib/chess_web/views/registration_view.ex
Normal file
@ -0,0 +1,3 @@
|
||||
defmodule ChessWeb.RegistrationView do
|
||||
use ChessWeb, :view
|
||||
end
|
||||
16
test/features/registration_test.exs
Normal file
16
test/features/registration_test.exs
Normal file
@ -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
|
||||
Loading…
Reference in New Issue
Block a user