Skip to content

Instantly share code, notes, and snippets.

@stephanos
Last active December 11, 2020 19:06
Show Gist options
  • Save stephanos/caafac3a40b26478eec74d09ac8b7bed to your computer and use it in GitHub Desktop.
Save stephanos/caafac3a40b26478eec74d09ac8b7bed to your computer and use it in GitHub Desktop.
defmodule Auth do
## IMPERATIVE SHELL
def login(email, input_password) do
with {:ok, user} <- UserRepo.find_by_email(email),
{:allowed, true} <- {:allowed, is_allowed(user, input_password)} do
UserRepo.update_last_login_date(user, DateTime.utc_now())
{:ok, user}
else
_ -> :error
end
end
## FUNCTIONAL CORE
defp is_allowed(user, input_password) do
password_hash = BCrypt.hash(input_password)
cond do
user.hash != hash -> false
user.active == false -> false
user.deleted_at != nil -> false
true -> true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment