Current section
Files
Jump to
Current section
Files
lib/maat_feather_web/controllers/auth_controller.ex
defmodule MaatFeatherWeb.AuthController do
@moduledoc """
Controller for authentication.
"""
use MaatFeatherWeb, :controller
alias MaatFeather.Users
alias MaatFeather.Users.User
alias MaatFeather.Auth.Token
alias MaatFeatherWeb.UserView
action_fallback MaatFeatherWeb.FallbackController
@doc """
Signin a user to the system.
"""
def signin(conn, %{"email" => email, "password" => password}) do
with %User{} = user <- Users.get_user_by!(email: email),
true <- Argon2.verify_pass(password, user.password_hash),
{:ok, token, _claims} <- Token.encode(user) do
conn
|> put_status(:ok)
|> json(%{token: token})
else
_ ->
{:error, :unauthorized}
end
end
@doc """
Get user profile.
"""
def profile(
%Plug.Conn{
assigns: %{user: %{"sub" => address}}
} = conn,
_params
) do
case Users.get_user!(address) do
nil ->
{:error, :not_found}
user ->
conn
|> put_view(UserView)
|> put_status(:ok)
|> render("show.json", %{user: user})
end
end
def profile(_conn, _params), do: {:error, :unauthorized}
end