Skip to content

Commit

Permalink
Bug fix: show logout template after logging out (#2913)
Browse files Browse the repository at this point in the history
hugobarauna authored Jan 20, 2025

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent 513935c commit a8d5786
Showing 7 changed files with 59 additions and 25 deletions.
5 changes: 1 addition & 4 deletions lib/livebook/config.ex
Original file line number Diff line number Diff line change
@@ -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 """
29 changes: 9 additions & 20 deletions lib/livebook_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/livebook_web/controllers/user_controller.ex
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions lib/livebook_web/controllers/user_html.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
defmodule LivebookWeb.UserHTML do
use LivebookWeb, :html

embed_templates "user_html/*"
end
2 changes: 1 addition & 1 deletion lib/livebook_web/router.ex
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions test/livebook_web/controllers/user_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a8d5786

Please sign in to comment.