2021-09-01 12:43:55 +00:00
|
|
|
defmodule ArchitexWeb.Client.RegisterController do
|
|
|
|
use ArchitexWeb, :controller
|
2021-06-25 15:43:12 +00:00
|
|
|
|
2021-09-01 12:43:55 +00:00
|
|
|
import ArchitexWeb.Error
|
2021-06-25 15:43:12 +00:00
|
|
|
|
2021-09-01 12:43:55 +00:00
|
|
|
alias Architex.{Repo, Account, Device}
|
|
|
|
alias ArchitexWeb.Client.Request.Register
|
2021-06-25 15:43:12 +00:00
|
|
|
|
2021-06-26 20:02:18 +00:00
|
|
|
@register_type "m.login.dummy"
|
|
|
|
|
2021-08-24 23:27:03 +00:00
|
|
|
@doc """
|
|
|
|
Register for an account on this homeserver.
|
|
|
|
|
|
|
|
Action for POST /_matrix/client/r0/register.
|
|
|
|
"""
|
2021-06-26 20:02:18 +00:00
|
|
|
def register(conn, %{"auth" => %{"type" => @register_type}} = params) do
|
2021-09-09 15:26:40 +00:00
|
|
|
with {:ok, %Register{inhibit_login: inhibit_login} = request} <- Register.parse(params) do
|
|
|
|
case Account.register(request) |> Repo.transaction() do
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
account: %Account{localpart: localpart},
|
|
|
|
device: %Device{id: device_id, access_token: access_token}
|
|
|
|
}} ->
|
|
|
|
data = %{user_id: Architex.get_mxid(localpart)}
|
2021-06-26 20:02:18 +00:00
|
|
|
|
2021-09-09 15:26:40 +00:00
|
|
|
data =
|
|
|
|
if not inhibit_login do
|
|
|
|
data
|
|
|
|
|> Map.put(:device_id, device_id)
|
|
|
|
|> Map.put(:access_token, access_token)
|
|
|
|
else
|
|
|
|
data
|
|
|
|
end
|
2021-06-26 20:02:18 +00:00
|
|
|
|
2021-09-09 15:26:40 +00:00
|
|
|
conn
|
|
|
|
|> put_status(200)
|
|
|
|
|> json(data)
|
2021-06-26 20:02:18 +00:00
|
|
|
|
2021-09-09 15:26:40 +00:00
|
|
|
{:error, _, cs, _} ->
|
|
|
|
put_error(conn, Register.get_error(cs))
|
|
|
|
end
|
|
|
|
else
|
2021-06-26 20:02:18 +00:00
|
|
|
_ ->
|
|
|
|
put_error(conn, :bad_json)
|
|
|
|
end
|
2021-06-25 15:43:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def register(conn, %{"auth" => _}) do
|
|
|
|
# Other login types are unsupported for now.
|
2021-07-29 14:59:40 +00:00
|
|
|
put_error(conn, :unrecognized, "Only m.login.dummy is supported currently.")
|
2021-06-25 15:43:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def register(conn, _params) do
|
|
|
|
# User has not started an auth flow.
|
|
|
|
data = %{
|
2021-06-26 20:02:18 +00:00
|
|
|
flows: [%{stages: [@register_type]}],
|
2021-06-25 15:43:12 +00:00
|
|
|
params: %{}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_status(401)
|
|
|
|
|> json(data)
|
|
|
|
end
|
|
|
|
end
|