-
-
Save pkoch/e2b94b01543334e1679452b0917ca3de 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
diff --git a/apps/artbay_api/test/controllers/auth_controller_test.exs b/apps/artbay_api/test/controllers/auth_controller_test.exs | |
index 9f82660..b39bd1a 100644 | |
--- a/apps/artbay_api/test/controllers/auth_controller_test.exs | |
+++ b/apps/artbay_api/test/controllers/auth_controller_test.exs | |
@@ -29,7 +29,7 @@ defmodule ArtbayAPI.AuthControllerTest do | |
end | |
test "logins user with valid attributes", %{conn: conn} do | |
- {:ok, _user} = Hermitage.Services.UserRegistry.register_user(@post_attrs) | |
+ {:ok, %User{}} = Hermitage.Services.UserRegistry.register_user(@post_attrs) | |
conn = post conn, auth_path(conn, :auth, :identity), @post_attrs | |
assert %{"status" => "ok", "token" => _} = json_response(conn, 200) | |
end | |
diff --git a/apps/hermitage/lib/hermitage/services/user_registry.ex b/apps/hermitage/lib/hermitage/services/user_registry.ex | |
index f7b492f..b0251b8 100644 | |
--- a/apps/hermitage/lib/hermitage/services/user_registry.ex | |
+++ b/apps/hermitage/lib/hermitage/services/user_registry.ex | |
@@ -23,27 +23,23 @@ defmodule Hermitage.Services.UserRegistry do | |
end | |
def register_user(params) do | |
- case validate_params_for_registration(params) do | |
- :ok -> | |
- create_user_with_authorization(params) | |
- {:error, reason} -> | |
- {:error, reason} | |
+ with :ok <- validate_params_for_registration(params), | |
+ {:ok, %User{} = user} <- create_user_with_authorization(params) do | |
+ {:ok, user} | |
end | |
end | |
def login_user(params) do | |
- case validate_params_for_login(params) do | |
- {:ok, changeset} -> | |
- case get_authorization_from_user_changeset(changeset) do | |
- {:error, reason} -> {:error, reason} | |
- authorization -> | |
- if authorization.expires_at && authorization.expires_at < Guardian.Utils.timestamp do | |
- replace_authorization(authorization, params) | |
- else | |
- get_user_from_authorization(authorization) | |
- end | |
- end | |
- {:error, reason} -> {:error, reason} | |
+ with {:ok, %Ecto.Changeset{} = changeset} <- | |
+ validate_params_for_login(params), | |
+ {:ok, authorization} <- | |
+ get_authorization_from_user_changeset(changeset) do | |
+ if( | |
+ authorization.expires_at && | |
+ authorization.expires_at < Guardian.Utils.timestamp, | |
+ do: replace_authorization(authorization, params), | |
+ else: get_user_from_authorization(authorization) | |
+ ) | |
end | |
end | |
@@ -70,15 +66,11 @@ defmodule Hermitage.Services.UserRegistry do | |
defp create_user_with_authorization(params) do | |
Repo.transaction(fn -> | |
- user = case create_passwordless_user(params) do | |
- {:ok, user} -> user | |
- {:error, reason} -> Repo.rollback(reason) | |
- end | |
- | |
- case create_authorization_for_user(user, params) do | |
- {:error, reason} -> Repo.rollback(reason) | |
- _ -> user | |
- end | |
+ with( | |
+ {:ok, %User{} = user} <- create_passwordless_user(params), | |
+ {:ok, %Authorization{}} <- create_authorization_for_user(user, params), | |
+ do: user | |
+ ) | |
end) | |
end | |
@@ -91,7 +83,7 @@ defmodule Hermitage.Services.UserRegistry do | |
end | |
def create_authorization_for_user(user, params) do | |
- result = create_authorization( | |
+ create_authorization( | |
%{ | |
user_id: user.id, | |
provider: "identity", | |
@@ -101,22 +93,12 @@ defmodule Hermitage.Services.UserRegistry do | |
expires_at: params["expires_at"] | |
} | |
) | |
- | |
- case result do | |
- {:ok, authorization} -> authorization | |
- {:error, reason} -> {:error, reason} | |
- end | |
end | |
def delete_authorization(%Authorization{} = authorization) do | |
Repo.delete(authorization) | |
end | |
- def get_authorization_by(query_params) do | |
- Authorization | |
- |> Repo.get_by(query_params) | |
- end | |
- | |
defp authorization_changeset(%Authorization{} = authorization, params) do | |
authorization | |
|> cast(params, [:provider, :uid, :user_id, :token, :refresh_token, :expires_at]) | |
@@ -129,21 +111,23 @@ defmodule Hermitage.Services.UserRegistry do | |
defp get_authorization_from_user_changeset(changeset) do | |
email = Ecto.Changeset.get_field(changeset, :email) | |
password = Ecto.Changeset.get_field(changeset, :password) | |
- case get_authorization_by(uid: email, provider: "identity") do | |
- nil -> | |
- changeset = Ecto.Changeset.add_error(changeset, :email, "Username not found") | |
- {:error, changeset} | |
- authorization -> | |
- case password do | |
- pass when is_binary(pass) -> | |
- if Comeonin.Bcrypt.checkpw(pass, authorization.token) do | |
- authorization | |
- else | |
- changeset = Ecto.Changeset.add_error(changeset, :password, "Password doesn't match'") | |
- {:error, changeset} | |
- end | |
- end | |
+ try do | |
+ {:ok, get_authorization(email, password)} | |
+ catch | |
+ {:error, {field, message}} -> | |
+ {:error, Ecto.Changeset.add_error(changeset, field, message)} | |
+ end | |
+ end | |
+ | |
+ defp get_authorization(email, password) when is_binary(password) do | |
+ auth = Repo.get_by(Authorization, uid: email, provider: "identity") | |
+ auth != nil or throw {:error, {:email, "Username not found"}} | |
+ | |
+ if !Comeonin.Bcrypt.checkpw(password, auth.token) do | |
+ throw {:error, {:password, "Password doesn't match"}} | |
end | |
+ | |
+ auth | |
end | |
defp get_user_from_authorization(authorization) do | |
@@ -154,16 +138,12 @@ defmodule Hermitage.Services.UserRegistry do | |
end | |
def replace_authorization(authorization, params) do | |
- case get_user_from_authorization(authorization) do | |
- {:ok, user} -> | |
- Repo.transaction(fn -> | |
+ with {:ok, %User{} = user} <- get_user_from_authorization(authorization), | |
+ {:ok, %Authorization{}} <- Repo.transaction(fn -> | |
delete_authorization(authorization) | |
- case create_authorization_for_user(user, params) do | |
- {:error, reason} -> Repo.rollback(reason) | |
- _ -> user | |
- end | |
- end) | |
- {:error, reason} -> {:error, reason} | |
+ create_authorization_for_user(user, params) | |
+ end) do | |
+ {:ok, user} | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment