Current section
Files
Jump to
Current section
Files
priv/templates/auth_live_session_list.eex
defmodule <%= inspect @module %> do
@moduledoc """
Session-management LiveView generated by Caravela from
<%= inspect @domain_module %>.
Mount this LiveView behind an authenticated `live_session` — it
requires `current_user` to be assigned by the `on_mount` hook.
"""
use <%= @web_module_alias %>, :live_view
alias <%= inspect @auth_module %>, as: Auth
@impl true
def mount(_params, session, socket) do
user = socket.assigns.current_user
current_token = Map.get(session || %{}, "user_token")
{:ok,
socket
|> assign(:current_token, current_token)
|> assign(:sessions, Auth.list_sessions(user, current_token))
|> assign(:loading, false)}
end
@impl true
def handle_event("revoke_session", %{"id" => id}, socket) do
user = socket.assigns.current_user
case Auth.revoke_session(user, id) do
{:ok, _} -> :ok
{:error, _} -> :ok
end
{:noreply,
assign(socket, :sessions, Auth.list_sessions(user, socket.assigns.current_token))}
end
def handle_event("revoke_all_others", _params, socket) do
user = socket.assigns.current_user
current = socket.assigns.current_token
for s <- Auth.list_sessions(user, current), not s.is_current do
Auth.revoke_session(user, s.id)
end
{:noreply, assign(socket, :sessions, Auth.list_sessions(user, current))}
end
# --- CUSTOM :auth_live_session_list ---
# --- END :auth_live_session_list ---
@impl true
def render(assigns) do
~H"""
<LiveSvelte.svelte
name="<%= @component_ref %>"
props={%{
current_user: @current_user,
sessions: @sessions,
loading: @loading
}}
socket={@socket}
/>
"""
end
<%= @custom_marker %>