mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Add name to user
This commit is contained in:
parent
4340d0e5bf
commit
c92b1b5aef
@ -6,9 +6,10 @@ defmodule Chess.Auth.User do
|
|||||||
alias Comeonin.Argon2
|
alias Comeonin.Argon2
|
||||||
|
|
||||||
schema "users" do
|
schema "users" do
|
||||||
|
field :name, :string
|
||||||
|
field :username, :string
|
||||||
field :password, :string, virtual: true
|
field :password, :string, virtual: true
|
||||||
field :password_hash, :string
|
field :password_hash, :string
|
||||||
field :username, :string
|
|
||||||
|
|
||||||
has_many :games, Chess.Store.Game
|
has_many :games, Chess.Store.Game
|
||||||
has_many :games_as_opponent, Chess.Store.Game, foreign_key: :opponent_id
|
has_many :games_as_opponent, Chess.Store.Game, foreign_key: :opponent_id
|
||||||
@ -19,8 +20,8 @@ defmodule Chess.Auth.User do
|
|||||||
@doc false
|
@doc false
|
||||||
def changeset(struct, params \\ %{}) do
|
def changeset(struct, params \\ %{}) do
|
||||||
struct
|
struct
|
||||||
|> cast(params, [:username, :password])
|
|> cast(params, required_attrs())
|
||||||
|> validate_required([:username, :password])
|
|> validate_required(required_attrs())
|
||||||
|> unique_constraint(:username)
|
|> unique_constraint(:username)
|
||||||
|> hash_password()
|
|> hash_password()
|
||||||
end
|
end
|
||||||
@ -34,4 +35,6 @@ defmodule Chess.Auth.User do
|
|||||||
changeset
|
changeset
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp required_attrs, do: ~w[name username password]a
|
||||||
end
|
end
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
<header role="banner">
|
<header role="banner">
|
||||||
<nav role="user">
|
<nav role="user">
|
||||||
<%= if current_user(@conn) do %>
|
<%= if current_user(@conn) do %>
|
||||||
<%= current_user(@conn).username %>
|
<%= current_user(@conn).name %> (<%= current_user(@conn).username %>)
|
||||||
|
|
|
|
||||||
<%= link("Log out", to: session_path(@conn, :delete), method: :delete) %>
|
<%= link("Log out", to: session_path(@conn, :delete), method: :delete) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
|
|||||||
@ -8,6 +8,9 @@
|
|||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
<div class="form-field">
|
||||||
|
<%= input f, :name %>
|
||||||
|
</div>
|
||||||
<div class="form-field">
|
<div class="form-field">
|
||||||
<%= input f, :username %>
|
<%= input f, :username %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
9
priv/repo/migrations/20180302195534_add_name_to_user.exs
Normal file
9
priv/repo/migrations/20180302195534_add_name_to_user.exs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
defmodule Chess.Repo.Migrations.AddNameToUser do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table("users") do
|
||||||
|
add :name, :string
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user