Bug fix: show logout template after logging out (#2913)

This commit is contained in:
Hugo Baraúna 2025-01-20 18:32:46 -03:00 committed by GitHub
parent 513935ce62
commit a8d57864cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 59 additions and 25 deletions

View file

@ -275,10 +275,7 @@ defmodule Livebook.Config do
def logout_enabled?() do
{_type, module, _key} = Livebook.Config.identity_provider()
identity_logout? =
Code.ensure_loaded?(module) and function_exported?(module, :logout, 2)
authentication().mode != :disabled or identity_logout?
Code.ensure_loaded?(module) and function_exported?(module, :logout, 2)
end
@doc """

View file

@ -46,26 +46,6 @@ defmodule LivebookWeb.AuthController do
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
conn
|> then(fn conn ->
@ -79,4 +59,13 @@ defmodule LivebookWeb.AuthController do
end)
|> halt()
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

View 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

View file

@ -0,0 +1,5 @@
defmodule LivebookWeb.UserHTML do
use LivebookWeb, :html
embed_templates "user_html/*"
end

View file

@ -171,7 +171,7 @@ defmodule LivebookWeb.Router do
scope "/", LivebookWeb do
pipe_through [:browser]
get "/logout", AuthController, :logout
get "/logout", UserController, :logout
end
defp within_iframe_secure_headers(conn, _opts) do

View 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