livebook/lib/livebook_web/controllers/auth_controller.ex

36 lines
745 B
Elixir
Raw Normal View History

2021-04-15 20:15:56 +08:00
defmodule LivebookWeb.AuthController do
use LivebookWeb, :controller
plug :require_unauthenticated_password
2021-04-15 20:15:56 +08:00
alias LivebookWeb.AuthPlug
2021-04-15 20:15:56 +08:00
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
2021-04-15 20:15:56 +08:00
end
def index(conn, _params) do
render(conn, "index.html")
2021-04-15 20:15:56 +08:00
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
2021-04-15 20:15:56 +08:00
defp redirect_home(conn) do
2021-04-15 20:15:56 +08:00
conn
|> redirect(to: "/")
|> halt()
2021-04-15 20:15:56 +08:00
end
end