From 155284139ef0e1e615a06bba0774ab6ee1eccd88 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Sat, 3 Mar 2018 12:28:51 -0500 Subject: [PATCH] Change username to email --- lib/chess/auth.ex | 4 +- lib/chess/auth/user.ex | 6 +- lib/chess_web/controllers/game_controller.ex | 2 +- .../controllers/session_controller.ex | 6 +- lib/chess_web/templates/game/index.html.eex | 2 +- lib/chess_web/templates/game/show.html.eex | 2 +- lib/chess_web/templates/layout/app.html.eex | 2 +- .../templates/registration/new.html.eex | 2 +- lib/chess_web/templates/session/new.html.eex | 2 +- ...0180303153745_change_username_to_email.exs | 9 ++ test/chess/auth/user_test.exs | 10 +- test/chess/auth_test.exs | 32 +++-- test/chess/store/game_test.exs | 12 +- .../controllers/api/game_controller_test.exs | 12 +- .../controllers/game_controller_test.exs | 8 +- test/features/games_test.exs | 116 ++++++++++++------ test/features/registration_test.exs | 2 +- test/features/session_test.exs | 22 ++-- test/support/factory.ex | 2 +- 19 files changed, 163 insertions(+), 90 deletions(-) create mode 100644 priv/repo/migrations/20180303153745_change_username_to_email.exs diff --git a/lib/chess/auth.ex b/lib/chess/auth.ex index 9c55709..4cce885 100644 --- a/lib/chess/auth.ex +++ b/lib/chess/auth.ex @@ -40,9 +40,9 @@ defmodule Chess.Auth do end @doc false - def authenticate_user(username, password) do + def authenticate_user(email, password) do query = from u in User, - where: u.username == ^username + where: u.email == ^email query |> Repo.one diff --git a/lib/chess/auth/user.ex b/lib/chess/auth/user.ex index 5148183..85391e8 100644 --- a/lib/chess/auth/user.ex +++ b/lib/chess/auth/user.ex @@ -7,7 +7,7 @@ defmodule Chess.Auth.User do schema "users" do field :name, :string - field :username, :string + field :email, :string field :password, :string, virtual: true field :password_hash, :string @@ -22,7 +22,7 @@ defmodule Chess.Auth.User do struct |> cast(params, required_attrs()) |> validate_required(required_attrs()) - |> unique_constraint(:username) + |> unique_constraint(:email) |> hash_password() end @@ -36,5 +36,5 @@ defmodule Chess.Auth.User do end end - defp required_attrs, do: ~w[name username password]a + defp required_attrs, do: ~w[name email password]a end diff --git a/lib/chess_web/controllers/game_controller.ex b/lib/chess_web/controllers/game_controller.ex index e26e6e3..549b10e 100644 --- a/lib/chess_web/controllers/game_controller.ex +++ b/lib/chess_web/controllers/game_controller.ex @@ -81,7 +81,7 @@ defmodule ChessWeb.GameController do defp get_opponents(user) do query = from user in "users", where: user.id != ^user.id, - select: {user.username, user.id} + select: {user.name, user.id} query |> Repo.all diff --git a/lib/chess_web/controllers/session_controller.ex b/lib/chess_web/controllers/session_controller.ex index 1c9c54f..5517490 100644 --- a/lib/chess_web/controllers/session_controller.ex +++ b/lib/chess_web/controllers/session_controller.ex @@ -12,9 +12,9 @@ defmodule ChessWeb.SessionController do def create( conn, - %{"user" => %{"username" => username, "password" => password}} + %{"user" => %{"email" => email, "password" => password}} ) do - case Auth.authenticate_user(username, password) do + case Auth.authenticate_user(email, password) do {:ok, user} -> conn |> Guardian.Plug.sign_in(user) @@ -23,7 +23,7 @@ defmodule ChessWeb.SessionController do {:error, _error} -> changeset = User.changeset(%User{}) conn - |> put_flash(:error, "Bad username or password") + |> put_flash(:error, "Bad email or password") |> render("new.html", changeset: changeset) end end diff --git a/lib/chess_web/templates/game/index.html.eex b/lib/chess_web/templates/game/index.html.eex index a9d71c3..36be170 100644 --- a/lib/chess_web/templates/game/index.html.eex +++ b/lib/chess_web/templates/game/index.html.eex @@ -5,7 +5,7 @@ <%= for game <- @games do %> - <%= link "Game with #{opponent(@conn, game).username}", to: game_path(@conn, :show, game), class: "btn btn-default btn-xs" %> + <%= link "Game with #{opponent(@conn, game).name}", to: game_path(@conn, :show, game), class: "btn btn-default btn-xs" %> Started on <%= Timex.format!(game.inserted_at, "%b %e at %I:%M %P", :strftime) %> diff --git a/lib/chess_web/templates/game/show.html.eex b/lib/chess_web/templates/game/show.html.eex index 0203eb8..51199a9 100644 --- a/lib/chess_web/templates/game/show.html.eex +++ b/lib/chess_web/templates/game/show.html.eex @@ -1,4 +1,4 @@ -

Game with <%= opponent(@conn, @game).username %>

+

Game with <%= opponent(@conn, @game).name %>

<%= link "Back to games", to: game_path(@conn, :index) %>

diff --git a/lib/chess_web/templates/layout/app.html.eex b/lib/chess_web/templates/layout/app.html.eex index 54b1120..8071437 100644 --- a/lib/chess_web/templates/layout/app.html.eex +++ b/lib/chess_web/templates/layout/app.html.eex @@ -16,7 +16,7 @@