Skip to content

Commit ceaac89

Browse files
author
Abdessabour Moutik
committed
feat: After user sign out all other tabs will disconnect
1 parent fe23af7 commit ceaac89

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

lib/mix/tasks/ash_authentication_phoenix.install.ex

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,20 @@ if Code.ensure_loaded?(Igniter) do
9595
"Which Phoenix router should be modified to allow authentication?"
9696
)
9797

98-
if router do
98+
with {igniter, router}
99+
when not is_nil(router) <-
100+
Igniter.Libs.Phoenix.select_router(
101+
igniter,
102+
"Which Phoenix router should be modified to allow authentication?"
103+
),
104+
{:endpoint, {igniter, endpoint}}
105+
when not is_nil(endpoint) <-
106+
{:endpoint,
107+
Igniter.Libs.Phoenix.select_endpoint(
108+
igniter,
109+
router,
110+
"Which Phoenix endpoint should be used to trigger disconnects?"
111+
)} do
99112
web_module = Igniter.Libs.Phoenix.web_module(igniter)
100113
overrides = Igniter.Libs.Phoenix.web_module_name(igniter, "AuthOverrides")
101114
otp_app = Igniter.Project.Application.app_name(igniter)
@@ -106,17 +119,26 @@ if Code.ensure_loaded?(Igniter) do
106119
|> setup_routes_alias()
107120
|> warn_on_missing_modules(options, argv, install?)
108121
|> do_or_explain_tailwind_changes()
109-
|> create_auth_controller(otp_app)
122+
|> create_auth_controller(otp_app, endpoint)
110123
|> create_overrides_module(overrides)
111124
|> add_auth_routes(overrides, options, router, web_module)
112125
|> create_live_user_auth(web_module)
113126
else
114-
igniter
115-
|> Igniter.add_warning("""
116-
AshAuthenticationPhoenix installer could not find a Phoenix router. Skipping installation.
127+
{igniter, nil} ->
128+
igniter
129+
|> Igniter.add_warning("""
130+
AshAuthenticationPhoenix installer could not find a Phoenix router. Skipping installation.
117131
118-
Set up a phoenix router and reinvoke the installer with `mix igniter.install ash_authentication_phoenix`.
119-
""")
132+
Set up a phoenix router and reinvoke the installer with `mix igniter.install ash_authentication_phoenix`.
133+
""")
134+
135+
{:endpoint, {igniter, nil}} ->
136+
igniter
137+
|> Igniter.add_warning("""
138+
AshAuthenticationPhoenix installer could not find a Phoenix endpoint. Skipping installation.
139+
140+
Set up a phoenix endpoint and reinvoke the installer with `mix igniter.install ash_authentication_phoenix`.
141+
""")
120142
end
121143
end
122144

@@ -326,15 +348,15 @@ if Code.ensure_loaded?(Igniter) do
326348
)
327349
end
328350

329-
defp create_auth_controller(igniter, otp_app) do
351+
defp create_auth_controller(igniter, otp_app, endpoint) do
330352
Igniter.Project.Module.create_module(
331353
igniter,
332354
Igniter.Libs.Phoenix.web_module_name(igniter, "AuthController"),
333355
"""
334356
use #{inspect(Igniter.Libs.Phoenix.web_module(igniter))}, :controller
335357
use AshAuthentication.Phoenix.Controller
336358
337-
def success(conn, activity, user, _token) do
359+
def success(conn, activity, user, token) do
338360
return_to = get_session(conn, :return_to) || ~p"/"
339361
340362
message =
@@ -344,9 +366,12 @@ if Code.ensure_loaded?(Igniter) do
344366
_ -> "You are now signed in"
345367
end
346368
369+
{:ok, %{"jti" => jti}} = AshAuthentication.Jwt.peek(token)
370+
347371
conn
348372
|> delete_session(:return_to)
349373
|> store_in_session(user)
374+
|> put_session(:live_socket_id, "users_socket:\#{jti}")
350375
# If your resource has a different name, update the assign name here (i.e :current_admin)
351376
|> assign(:current_user, user)
352377
|> put_flash(:info, message)
@@ -379,10 +404,14 @@ if Code.ensure_loaded?(Igniter) do
379404
def sign_out(conn, _params) do
380405
return_to = get_session(conn, :return_to) || ~p"/"
381406
407+
live_socket_id =
408+
get_session(conn, :live_socket_id)
409+
382410
conn
383411
|> clear_session(:#{otp_app})
384412
|> put_flash(:info, "You are now signed out")
385413
|> redirect(to: return_to)
414+
|> tap(fn _ -> #{inspect(endpoint)}.broadcast(live_socket_id, "disconnect", %{}) end)
386415
end
387416
"""
388417
)

0 commit comments

Comments
 (0)