livebook/lib/livebook_web/controllers/auth_controller.ex
José Valim c4d06d877e Move auth to its own view
Prior to this PR the HTML head was being
included twice on the password auth page.
One from root.html and another from the
error page, so we decoupled those.
2021-04-16 22:18:59 +02:00

35 lines
745 B
Elixir

defmodule LivebookWeb.AuthController do
use LivebookWeb, :controller
plug :require_unauthenticated_password
alias LivebookWeb.AuthPlug
defp require_unauthenticated_password(conn, _opts) do
if Livebook.Config.auth_mode() != :password or AuthPlug.authenticated?(conn, :password) do
redirect_home(conn)
else
conn
end
end
def index(conn, _params) do
render(conn, "index.html")
end
def authenticate(conn, %{"password" => password}) do
conn = AuthPlug.store(conn, :password, password)
if AuthPlug.authenticated?(conn, :password) do
redirect_home(conn)
else
index(conn, %{})
end
end
defp redirect_home(conn) do
conn
|> redirect(to: "/")
|> halt()
end
end