     Bcrypt.checkpw(password, user.password_hash) && {:ok, user} || {:error, :wrong_credentials}
   end
 
-  defp sign_user({:error, error}), do: {:error, error}
+  defp sign_user({:error, error}), do: error
   defp sign_user({:ok, user}) do
     {:ok, jwt, _} = Guardian.encode_and_sign(
       user,
       :token,
       perms: %{"#{user.role}": Permissions.max},
     )
-    {:ok, jwt}
+    {:ok, jwt, preload_user_data(user)}
   end
 
-  defp revoke_claims(token) do
-    Guardian.revoke!(token)
+  defp revoke_claims(conn) do
+    {:ok, claims} = Plug.claims(conn)
+
+    Plug.current_token(conn)
+    |> Guardian.revoke!(claims)
+
+    conn
   end
 
-  defp add_pwd_recovery_data(nil), do: {:ok, nil} # do not leak existent / inexistent emails
+  defp add_pwd_recovery_data(nil), do: :user_not_found
   defp add_pwd_recovery_data(user) do
     changeset = User.changeset(user, %{
-      pwd_recovery_token: User.generate_token(),
-      pwd_recovery_token_expiration: User.calculate_token_expiration()
+      pwd_recovery_token: UserHelper.generate_token(),
+      pwd_recovery_token_expiration: UserHelper.calculate_token_expiration()
     })
 
     case Repo.update(changeset) do
       {:ok, user} ->
-        Emails.recover_password_email(user) |> Mailer.deliver_later
+        Email.recover_password_email(user) |> Mailer.deliver_later
         {:ok, user}
-      {:error, changeset} -> {:ok, nil} # do not leak existent / inexistent emails
+      {:error, changeset} -> {:error, changeset}
     end
   end
 


    case Repo.insert(changeset) do
      {:ok, user} ->
        Teams.associate_invites_with_user(user.email, user.id)

        Emails.registration_email(user) |> Mailer.deliver_later

        {:ok, jwt, _claims} = Guardian.encode_and_sign(user, :token)

        {:ok, jwt}
      {:error, changeset} -> {:error, changeset}
    end
  end

  def update_current_user(current_user, params) do
    changeset = User.participant_changeset(current_user, params)
    {:ok, Repo.update(changeset)}
  end

  def update_user(current_user, id, params) do
    user = get_user(id)

    if user.id == current_user.id do
      changeset = User.participant_changeset(user, params)
      Repo.update(changeset)
    else
      {:error, :unauthorized}
    end
  end

  def update_any_user(id, params) do
    user = get_user(id)

    changeset = User.admin_changeset(user, params)
    Repo.update(changeset)
  end

  def delete_user(current_user, id) do
    user = Repo.get!(User, id)

    if user.id == current_user.id do
      Repo.delete(user)
    else
      {:unauthorized, :unauthorized}
    end
  end

  def delete_any_user(id) do
    user = get_user(id)
    Repo.delete(user)
  end

  def get_pwd_token(email) do
    Repo.get_by(User, email: email)
    |> add_pwd_recovery_data()
  end

    |> sign_user
  end

  def delete_session(conn) do
    revoke_claims(conn)
  end

  def list_users do
    Enum.map(Repo.all(User), fn(user) -> preload_user_data(user) end)
  end

  def get_user(id) do
    Repo.get!(User, id)
    |> preload_user_data
  end

  def create_user(user_params) do
