index.community/backend/lib/backend_web/controllers/admin_login_controller.ex

47 lines
1.4 KiB
Elixir
Raw Normal View History

2019-07-26 14:34:23 +00:00
defmodule BackendWeb.AdminLoginController do
use BackendWeb, :controller
import Backend.Util
alias Backend.Mailer.UserEmail
action_fallback BackendWeb.FallbackController
@doc """
Given an instance, looks up the login types (email or admin account) and returns them. The user can then
choose one or the other by POSTing back.
"""
def show(conn, %{"id" => domain}) do
# TODO: this should really be handled in a more async manner
# TODO: this assumes mastodon/pleroma API
cleaned_domain = clean_domain(domain)
instance_data = get_and_decode!("https://#{cleaned_domain}/api/v1/instance")
2019-07-26 14:34:23 +00:00
render(conn, "show.json", instance_data: instance_data, cleaned_domain: cleaned_domain)
end
def create(conn, %{"domain" => domain, "type" => type}) do
cleaned_domain = clean_domain(domain)
instance_data = get_and_decode!("https://#{cleaned_domain}/api/v1/instance")
2019-07-26 14:34:23 +00:00
error =
cond do
type == "email" ->
email = Map.get(instance_data, "email")
case UserEmail.send_login_email(email, cleaned_domain) do
{:ok, _} -> nil
{:error, _} -> "Failed to send email."
end
# type == "fediverseAccount" ->
# account = nil
true ->
"Invalid account type. Must be 'email' or 'fediverseAccount'."
end
render(conn, "create.json", error: error)
end
end