mirror of
https://github.com/livebook-dev/livebook.git
synced 2025-10-06 03:34:57 +08:00
Bug fix: show logout template after logging out (#2913)
This commit is contained in:
parent
513935ce62
commit
a8d57864cf
7 changed files with 59 additions and 25 deletions
|
@ -275,10 +275,7 @@ defmodule Livebook.Config do
|
||||||
def logout_enabled?() do
|
def logout_enabled?() do
|
||||||
{_type, module, _key} = Livebook.Config.identity_provider()
|
{_type, module, _key} = Livebook.Config.identity_provider()
|
||||||
|
|
||||||
identity_logout? =
|
Code.ensure_loaded?(module) and function_exported?(module, :logout, 2)
|
||||||
Code.ensure_loaded?(module) and function_exported?(module, :logout, 2)
|
|
||||||
|
|
||||||
authentication().mode != :disabled or identity_logout?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -46,26 +46,6 @@ defmodule LivebookWeb.AuthController do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def logout(conn, _params) do
|
|
||||||
if get_session(conn, :user_id) do
|
|
||||||
conn
|
|
||||||
|> configure_session(renew: true)
|
|
||||||
|> clear_session()
|
|
||||||
|> render("logout.html")
|
|
||||||
else
|
|
||||||
redirect_to(conn)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp render_form_error(conn, authentication_mode) do
|
|
||||||
errors = [{"%{authentication_mode} is invalid", [authentication_mode: authentication_mode]}]
|
|
||||||
|
|
||||||
render(conn, "index.html",
|
|
||||||
errors: errors,
|
|
||||||
authentication_mode: authentication_mode
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
defp redirect_to(conn) do
|
defp redirect_to(conn) do
|
||||||
conn
|
conn
|
||||||
|> then(fn conn ->
|
|> then(fn conn ->
|
||||||
|
@ -79,4 +59,13 @@ defmodule LivebookWeb.AuthController do
|
||||||
end)
|
end)
|
||||||
|> halt()
|
|> halt()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp render_form_error(conn, authentication_mode) do
|
||||||
|
errors = [{"%{authentication_mode} is invalid", [authentication_mode: authentication_mode]}]
|
||||||
|
|
||||||
|
render(conn, "index.html",
|
||||||
|
errors: errors,
|
||||||
|
authentication_mode: authentication_mode
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
14
lib/livebook_web/controllers/user_controller.ex
Normal file
14
lib/livebook_web/controllers/user_controller.ex
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
defmodule LivebookWeb.UserController do
|
||||||
|
use LivebookWeb, :controller
|
||||||
|
|
||||||
|
def logout(conn, _params) do
|
||||||
|
if get_session(conn, :user_id) do
|
||||||
|
conn
|
||||||
|
|> configure_session(renew: true)
|
||||||
|
|> clear_session()
|
||||||
|
|> render("logout.html")
|
||||||
|
else
|
||||||
|
redirect(conn, to: ~p"/")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
5
lib/livebook_web/controllers/user_html.ex
Normal file
5
lib/livebook_web/controllers/user_html.ex
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
defmodule LivebookWeb.UserHTML do
|
||||||
|
use LivebookWeb, :html
|
||||||
|
|
||||||
|
embed_templates "user_html/*"
|
||||||
|
end
|
|
@ -171,7 +171,7 @@ defmodule LivebookWeb.Router do
|
||||||
|
|
||||||
scope "/", LivebookWeb do
|
scope "/", LivebookWeb do
|
||||||
pipe_through [:browser]
|
pipe_through [:browser]
|
||||||
get "/logout", AuthController, :logout
|
get "/logout", UserController, :logout
|
||||||
end
|
end
|
||||||
|
|
||||||
defp within_iframe_secure_headers(conn, _opts) do
|
defp within_iframe_secure_headers(conn, _opts) do
|
||||||
|
|
29
test/livebook_web/controllers/user_controller_test.exs
Normal file
29
test/livebook_web/controllers/user_controller_test.exs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
defmodule LivebookWeb.UserControllerTest do
|
||||||
|
use LivebookWeb.ConnCase, async: true
|
||||||
|
|
||||||
|
describe "GET /logout" do
|
||||||
|
test "renders logout template when logged in", %{conn: conn} do
|
||||||
|
conn = login_user(conn)
|
||||||
|
|
||||||
|
conn = get(conn, ~p"/logout")
|
||||||
|
|
||||||
|
assert html_response(conn, 200) =~ "You have been logged out"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "redirects when already logged out", %{conn: conn} do
|
||||||
|
conn = logout_user(conn)
|
||||||
|
|
||||||
|
conn = get(conn, ~p"/logout")
|
||||||
|
|
||||||
|
assert redirected_to(conn) == ~p"/"
|
||||||
|
end
|
||||||
|
|
||||||
|
defp login_user(conn) do
|
||||||
|
Phoenix.ConnTest.init_test_session(conn, %{user_id: 1})
|
||||||
|
end
|
||||||
|
|
||||||
|
defp logout_user(conn) do
|
||||||
|
Phoenix.ConnTest.init_test_session(conn, %{})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Reference in a new issue