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

46 lines
1.3 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
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
2019-08-21 12:30:47 +00:00
# credo:disable-for-lines:16 Credo.Check.Refactor.CondStatements
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