     |> sign_user
   end
 
-  def delete_session(token) do
-    revoke_claims(token)
+  def delete_session(conn) do
+    revoke_claims(conn)
   end
 
   def list_users do
-    Repo.all(User)
+    Enum.map(Repo.all(User), fn(user) -> preload_user_data(user) end)
   end
 
   def get_user(id) do
-    Repo.get(User, id)
+    Repo.get!(User, id)
+    |> preload_user_data
   end
 
   def create_user(user_params) do


  alias Api.{Mailer, Repo}
  alias Api.Accounts.User
  alias Api.Teams
  alias Api.Notifications.Emails
  alias Comeonin.Bcrypt
  alias Guardian.Permissions

  def create_session(email, password) do
    Repo.get_by(User, email: email)

    Bcrypt.checkpw(password, user.password_hash) && {:ok, user} || {:error, :wrong_credentials}
  end

  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, preload_user_data(user)}
  end

  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: :user_not_found
  defp add_pwd_recovery_data(user) do
    changeset = User.changeset(user, %{
      pwd_recovery_token: UserHelper.generate_token(),
      pwd_recovery_token_expiration: UserHelper.calculate_token_expiration()
    })

    case Repo.update(changeset) do
      {:ok, user} ->
        Email.recover_password_email(user) |> Mailer.deliver_later
        {:ok, user}
      {:error, changeset} -> {:error, changeset}
    end
  end

