Skip to content

Instantly share code, notes, and snippets.

@joeljuca
Created July 31, 2024 18:30
Show Gist options
  • Save joeljuca/0b13ccfe6373989f2551ddec8ba51fd2 to your computer and use it in GitHub Desktop.
Save joeljuca/0b13ccfe6373989f2551ddec8ba51fd2 to your computer and use it in GitHub Desktop.
defmodule MyApp.Accounts do
# (...)
def sign_up(%{} = params) do
params_schema = %{
name: :string,
email: :string,
phone: :string,
password: :string
}
changeset =
{%{}, params_schema}
|> cast(params, Map.keys(params_schema))
|> validate_required([:name, :email, :password])
|> validate_length(:password, min: 24)
|> validate_confirmation(:password, required: true)
with {:ok, params} <- apply_action(changeset, :sign_up),
{:ok, pw_hash} <- some_password_magic_goes_here(params.password),
{:ok, user} <- User.create(params |> Map.put(:password_hash, pw_hash)) do
{:ok, user}
end
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment