Skip to content

Commit 78aebde

Browse files
authored
Allow user to logout from Livebook app (#2961)
1 parent e52ed86 commit 78aebde

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

lib/livebook_web/helpers/session_helpers.ex

+28
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,32 @@ defmodule LivebookWeb.SessionHelpers do
274274
Livebook.Session.register_file(session_pid, path, key)
275275
end
276276
end
277+
278+
@logout_topic "logout"
279+
280+
@doc """
281+
Subscribes to #{@logout_topic} topic.
282+
"""
283+
@spec subscribe_to_logout() :: :ok | {:error, term()}
284+
def subscribe_to_logout do
285+
Phoenix.PubSub.subscribe(Livebook.PubSub, @logout_topic)
286+
end
287+
288+
@doc """
289+
Shows the confirmation modal to logout user from Livebook.
290+
"""
291+
@spec confirm_logout(Socket.t()) :: Socket.t()
292+
def confirm_logout(socket) do
293+
on_confirm = fn socket ->
294+
Phoenix.PubSub.broadcast(Livebook.PubSub, @logout_topic, :logout)
295+
put_flash(socket, :info, "Livebook is logging out. You will be redirected soon.")
296+
end
297+
298+
confirm(socket, on_confirm,
299+
title: "Log out",
300+
description: "Are you sure you want to log out Livebook now?",
301+
confirm_text: "Log out",
302+
confirm_icon: "logout-box-line"
303+
)
304+
end
277305
end

lib/livebook_web/live/app_session_live.ex

+6
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ defmodule LivebookWeb.AppSessionLive do
140140
<span>Debug</span>
141141
</.link>
142142
</.menu_item>
143+
<.menu_item :if={Livebook.Config.logout_enabled?() and @current_user.email != nil}>
144+
<button phx-click="logout" role="menuitem">
145+
<.remix_icon icon="logout-box-line" />
146+
<span>Logout</span>
147+
</button>
148+
</.menu_item>
143149
</.menu>
144150
</div>
145151
<div data-el-js-view-iframes phx-update="ignore" id="js-view-iframes"></div>

lib/livebook_web/live/hooks/app_auth_hook.ex

+21-1
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,17 @@ defmodule LivebookWeb.AppAuthHook do
4141
#
4242

4343
def on_mount(:default, %{"slug" => slug}, session, socket) do
44+
if connected?(socket) do
45+
LivebookWeb.SessionHelpers.subscribe_to_logout()
46+
end
47+
4448
livebook_authenticated? = livebook_authenticated?(session, socket)
4549

46-
socket = assign(socket, livebook_authenticated?: livebook_authenticated?)
50+
socket =
51+
socket
52+
|> assign(livebook_authenticated?: livebook_authenticated?)
53+
|> attach_hook(:logout, :handle_info, &handle_info/2)
54+
|> attach_hook(:logout, :handle_event, &handle_event/3)
4755

4856
case Livebook.Apps.fetch_settings(slug) do
4957
{:ok, %{access_type: :public} = app_settings} ->
@@ -70,6 +78,18 @@ defmodule LivebookWeb.AppAuthHook do
7078
LivebookWeb.AuthPlug.authenticated?(session, uri.port)
7179
end
7280

81+
defp handle_info(:logout, socket) do
82+
{:halt, redirect(socket, to: ~p"/logout")}
83+
end
84+
85+
defp handle_info(_event, socket), do: {:cont, socket}
86+
87+
defp handle_event("logout", %{}, socket) do
88+
{:halt, LivebookWeb.SessionHelpers.confirm_logout(socket)}
89+
end
90+
91+
defp handle_event(_event, _params, socket), do: {:cont, socket}
92+
7393
defp has_valid_token?(socket, app_settings) do
7494
connect_params = get_connect_params(socket) || %{}
7595

lib/livebook_web/live/hooks/sidebar_hook.ex

+2-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule LivebookWeb.SidebarHook do
99
def on_mount(:default, _params, _session, socket) do
1010
if connected?(socket) do
1111
Livebook.Hubs.Broadcasts.subscribe([:crud, :connection])
12+
LivebookWeb.SessionHelpers.subscribe_to_logout()
1213
Phoenix.PubSub.subscribe(Livebook.PubSub, "sidebar")
1314
end
1415

@@ -67,18 +68,7 @@ defmodule LivebookWeb.SidebarHook do
6768
end
6869

6970
defp handle_event("logout", _params, socket) do
70-
on_confirm = fn socket ->
71-
Phoenix.PubSub.broadcast(Livebook.PubSub, "sidebar", :logout)
72-
put_flash(socket, :info, "Livebook is logging out. You will be redirected soon.")
73-
end
74-
75-
{:halt,
76-
confirm(socket, on_confirm,
77-
title: "Log out",
78-
description: "Are you sure you want to log out Livebook now?",
79-
confirm_text: "Log out",
80-
confirm_icon: "logout-box-line"
81-
)}
71+
{:halt, LivebookWeb.SessionHelpers.confirm_logout(socket)}
8272
end
8373

8474
defp handle_event(_event, _params, socket), do: {:cont, socket}

0 commit comments

Comments
 (0)