Last active
December 11, 2020 19:06
-
-
Save stephanos/caafac3a40b26478eec74d09ac8b7bed 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 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