Update user gen

This commit is contained in:
Ondřej 2024-08-09 01:28:57 +02:00
parent 564c11b2d3
commit beb9d0f112
3 changed files with 34 additions and 21 deletions

View file

@ -88,9 +88,9 @@ defmodule ChoreTracker.Accounts do
{:error, %Ecto.Changeset{}}
"""
def register_user(attrs) do
def register_user(attrs, opts) do
%User{}
|> User.registration_changeset(attrs)
|> User.registration_changeset(attrs, opts)
|> Repo.insert()
end

View file

@ -54,7 +54,13 @@ defmodule ChoreTracker.Accounts.User do
defp validate_password(changeset, opts) do
changeset
|> validate_required([:password])
|> validate_length(:password, min: 12, max: 72)
|> then(fn changeset ->
if opts[:disable_password_validation] do
changeset
else
changeset |> validate_length(:password, min: 12, max: 72)
end
end)
# Examples of additional password validation:
# |> validate_format(:password, ~r/[a-z]/, message: "at least one lower case character")
# |> validate_format(:password, ~r/[A-Z]/, message: "at least one upper case character")

View file

@ -10,28 +10,35 @@
# We recommend using the bang functions (`insert!`, `update!`
# and so on) as they will fail if something goes wrong.
alias ChoreTracker.Helpers
admin_password = Helpers.random_string(16)
admin_password = "secret"
{:ok, admin} =
ChoreTracker.Accounts.register_user(%{
email: "admin@jednota.party",
password: admin_password,
display_name: "Admin"
})
ChoreTracker.Accounts.register_user(
%{
email: "ondrej@jednota.party",
password: admin_password,
display_name: "Ondřej"
},
disable_password_validation: true
)
ChoreTracker.Accounts.register_user(%{
email: "matej@jednota.party",
password: Helpers.random_string(16),
display_name: "Matěj"
})
ChoreTracker.Accounts.register_user(
%{
email: "matej@jednota.party",
password: "secret",
display_name: "Matěj"
},
disable_password_validation: true
)
ChoreTracker.Accounts.register_user(%{
email: "valentyna@jednota.party",
password: Helpers.random_string(16),
display_name: "Valentýna"
})
ChoreTracker.Accounts.register_user(
%{
email: "valentyna@jednota.party",
password: "secret",
display_name: "Valentýna"
},
disable_password_validation: true
)
ChoreTracker.Chores.create_chore(%{
name: "Uklidit kuchyň",