architex/lib/architex_web/client/controllers/account_controller.ex

72 lines
1.6 KiB
Elixir
Raw Normal View History

2021-09-01 12:43:55 +00:00
defmodule ArchitexWeb.Client.AccountController do
use ArchitexWeb, :controller
2021-09-01 12:43:55 +00:00
import ArchitexWeb.Error
2021-09-01 12:43:55 +00:00
alias Architex.{Account, Repo}
alias Plug.Conn
@doc """
Checks to see if a username is available, and valid, for the server.
Action for GET /_matrix/client/r0/register/available.
"""
def available(conn, params) do
localpart = Map.get(params, "username", "")
2021-06-25 23:14:09 +00:00
case Account.available?(localpart) do
:ok ->
conn
|> put_status(200)
|> json(%{available: true})
2021-06-25 23:14:09 +00:00
{:error, error} ->
put_error(conn, error)
end
end
@doc """
Gets information about the owner of a given access token.
Action for GET /_matrix/client/r0/account/whoami.
"""
def whoami(%Conn{assigns: %{account: %Account{localpart: localpart}}} = conn, _params) do
data = %{user_id: Architex.get_mxid(localpart)}
conn
|> put_status(200)
|> json(data)
end
@doc """
Invalidates an existing access token, so that it can no longer be used for authorization.
Action for POST /_matrix/client/r0/logout.
"""
def logout(%Conn{assigns: %{device: device}} = conn, _params) do
case Repo.delete(device) do
{:ok, _} ->
conn
|> put_status(200)
|> json(%{})
{:error, _} ->
put_error(conn, :unknown)
end
end
@doc """
Invalidates all access tokens for a user, so that they can no longer be used
for authorization.
Action for POST /_matrix/client/r0/logout/all.
"""
def logout_all(%Conn{assigns: %{account: account}} = conn, _params) do
Repo.delete_all(Ecto.assoc(account, :devices))
conn
|> put_status(200)
|> json(%{})
end
end