Created
July 31, 2024 18:30
-
-
Save joeljuca/0b13ccfe6373989f2551ddec8ba51fd2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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