mirror of
https://codeberg.org/JasterV/spazio-solazzo.git
synced 2026-04-26 18:20:03 +00:00
* refactor: update button colors * feat: add dashboard panel for admins only & update header styles
49 lines
1.4 KiB
Elixir
49 lines
1.4 KiB
Elixir
defmodule SpazioSolazzoWeb.LiveUserAuth do
|
|
@moduledoc """
|
|
Helpers for authenticating users in LiveViews.
|
|
"""
|
|
|
|
import Phoenix.Component
|
|
use SpazioSolazzoWeb, :verified_routes
|
|
|
|
# This is used for nested liveviews to fetch the current user.
|
|
# To use, place the following at the top of that liveview:
|
|
# on_mount {SpazioSolazzoWeb.LiveUserAuth, :current_user}
|
|
def on_mount(:current_user, _params, session, socket) do
|
|
{:cont, AshAuthentication.Phoenix.LiveSession.assign_new_resources(socket, session)}
|
|
end
|
|
|
|
def on_mount(:live_user_optional, _params, _session, socket) do
|
|
if socket.assigns[:current_user] do
|
|
{:cont, socket}
|
|
else
|
|
{:cont, assign(socket, :current_user, nil)}
|
|
end
|
|
end
|
|
|
|
def on_mount(:live_user_required, _params, _session, socket) do
|
|
if socket.assigns[:current_user] do
|
|
{:cont, socket}
|
|
else
|
|
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/sign-in")}
|
|
end
|
|
end
|
|
|
|
def on_mount(:live_no_user, _params, _session, socket) do
|
|
if socket.assigns[:current_user] do
|
|
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/")}
|
|
else
|
|
{:cont, assign(socket, :current_user, nil)}
|
|
end
|
|
end
|
|
|
|
def on_mount(:live_admin_required, _params, _session, socket) do
|
|
case socket.assigns[:current_user] do
|
|
%{is_admin: true} ->
|
|
{:cont, socket}
|
|
|
|
_ ->
|
|
{:halt, Phoenix.LiveView.redirect(socket, to: ~p"/")}
|
|
end
|
|
end
|
|
end
|