feat(chat): validate username on registration
Also, update seeds to register with usernames.
This commit is contained in:
parent
10c905249c
commit
b7de53bab0
@ -94,7 +94,11 @@ defmodule Slax.Accounts do
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
def change_user_registration(%User{} = user, attrs \\ %{}) do
|
def change_user_registration(%User{} = user, attrs \\ %{}) do
|
||||||
User.registration_changeset(user, attrs, hash_password: false, validate_email: false)
|
User.registration_changeset(user, attrs,
|
||||||
|
hash_password: false,
|
||||||
|
validate_email: false,
|
||||||
|
validate_username: false
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
## Settings
|
## Settings
|
||||||
|
@ -43,8 +43,9 @@ defmodule Slax.Accounts.User do
|
|||||||
"""
|
"""
|
||||||
def registration_changeset(user, attrs, opts \\ []) do
|
def registration_changeset(user, attrs, opts \\ []) do
|
||||||
user
|
user
|
||||||
|> cast(attrs, [:email, :password])
|
|> cast(attrs, [:email, :username, :password])
|
||||||
|> validate_email(opts)
|
|> validate_email(opts)
|
||||||
|
|> validate_username(opts)
|
||||||
|> validate_password(opts)
|
|> validate_password(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -56,6 +57,36 @@ defmodule Slax.Accounts.User do
|
|||||||
|> maybe_validate_unique_email(opts)
|
|> maybe_validate_unique_email(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp maybe_validate_unique_email(changeset, opts) do
|
||||||
|
if Keyword.get(opts, :validate_email, true) do
|
||||||
|
changeset
|
||||||
|
|> unsafe_validate_unique(:email, Slax.Repo)
|
||||||
|
|> unique_constraint(:email)
|
||||||
|
else
|
||||||
|
changeset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp validate_username(changeset, opts) do
|
||||||
|
changeset
|
||||||
|
|> validate_required([:username])
|
||||||
|
|> validate_format(:username, ~r/^[A-Za-z0-9-]+$/,
|
||||||
|
message: "can only contain letters, numbers, and dashes"
|
||||||
|
)
|
||||||
|
|> validate_length(:username, max: 20)
|
||||||
|
|> maybe_validate_unique_username(opts)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_validate_unique_username(changeset, opts) do
|
||||||
|
if Keyword.get(opts, :validate_username, true) do
|
||||||
|
changeset
|
||||||
|
|> unsafe_validate_unique(:username, Slax.Repo)
|
||||||
|
|> unique_constraint(:username)
|
||||||
|
else
|
||||||
|
changeset
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp validate_password(changeset, opts) do
|
defp validate_password(changeset, opts) do
|
||||||
changeset
|
changeset
|
||||||
|> validate_required([:password])
|
|> validate_required([:password])
|
||||||
@ -84,16 +115,6 @@ defmodule Slax.Accounts.User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_validate_unique_email(changeset, opts) do
|
|
||||||
if Keyword.get(opts, :validate_email, true) do
|
|
||||||
changeset
|
|
||||||
|> unsafe_validate_unique(:email, Slax.Repo)
|
|
||||||
|> unique_constraint(:email)
|
|
||||||
else
|
|
||||||
changeset
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
A user changeset for changing the email.
|
A user changeset for changing the email.
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ pw = "TheFellowship"
|
|||||||
|
|
||||||
for name <- names do
|
for name <- names do
|
||||||
email = "#{String.downcase(name)}@fellowship.me"
|
email = "#{String.downcase(name)}@fellowship.me"
|
||||||
Accounts.register_user(%{email: email, password: pw, password_confirmation: pw})
|
Accounts.register_user(%{username: name, email: email, password: pw, password_confirmation: pw})
|
||||||
end
|
end
|
||||||
|
|
||||||
aragorn = Accounts.get_user_by_email("aragorn@fellowship.me")
|
aragorn = Accounts.get_user_by_email("aragorn@fellowship.me")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user