architex/lib/matrix_server_web/client/controllers/register_controller.ex

70 lines
1.7 KiB
Elixir
Raw Normal View History

2021-08-06 21:14:27 +00:00
defmodule MatrixServerWeb.Client.RegisterController do
use MatrixServerWeb, :controller
import MatrixServerWeb.Error
2021-07-10 21:16:00 +00:00
import Ecto.Changeset
alias MatrixServer.{Repo, Account, Device}
alias MatrixServerWeb.Client.Request.Register
alias Ecto.Changeset
2021-06-26 20:02:18 +00:00
@register_type "m.login.dummy"
@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
case Register.changeset(params) do
%Changeset{valid?: true} = cs ->
2021-07-17 15:38:20 +00:00
input = apply_changes(cs)
2021-06-26 20:02:18 +00:00
2021-07-17 15:38:20 +00:00
case Account.register(input) |> Repo.transaction() do
{:ok,
%{
account: %Account{localpart: localpart},
device: %Device{device_id: device_id, access_token: access_token}
}} ->
data = %{user_id: MatrixServer.get_mxid(localpart)}
2021-06-26 20:02:18 +00:00
data =
2021-07-17 15:38:20 +00:00
if not input.inhibit_login do
2021-06-26 20:02:18 +00:00
data
|> Map.put(:device_id, device_id)
|> Map.put(:access_token, access_token)
2021-06-26 20:02:18 +00:00
else
data
end
conn
|> put_status(200)
|> json(data)
{:error, _, cs, _} ->
2021-07-10 21:16:00 +00:00
put_error(conn, Register.get_error(cs))
2021-06-26 20:02:18 +00:00
end
_ ->
put_error(conn, :bad_json)
end
end
def register(conn, %{"auth" => _}) do
# Other login types are unsupported for now.
put_error(conn, :unrecognized, "Only m.login.dummy is supported currently.")
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]}],
params: %{}
}
conn
|> put_status(401)
|> json(data)
end
end