2022-08-07 11:22:58 +12:00
|
|
|
defmodule AshHqWeb.LiveUserAuth do
|
|
|
|
@moduledoc """
|
|
|
|
Helpers for authenticating users in liveviews
|
|
|
|
"""
|
|
|
|
|
2022-08-16 13:23:59 +12:00
|
|
|
alias AshHqWeb.Router.Helpers, as: Routes
|
|
|
|
|
2022-08-07 11:22:58 +12:00
|
|
|
@doc """
|
|
|
|
Sets the current user on each mount of a liveview
|
|
|
|
"""
|
|
|
|
def on_mount(:live_user, _params, session, socket) do
|
|
|
|
{:cont,
|
|
|
|
Phoenix.LiveView.assign(
|
|
|
|
socket,
|
|
|
|
:current_user,
|
|
|
|
AshHqWeb.UserAuth.user_for_session_token(session["user_token"])
|
|
|
|
)}
|
|
|
|
end
|
2022-08-16 13:23:59 +12:00
|
|
|
|
|
|
|
def on_mount(:live_user_required, _params, session, socket) do
|
|
|
|
case AshHqWeb.UserAuth.user_for_session_token(session["user_token"]) do
|
|
|
|
nil ->
|
|
|
|
{:halt, Phoenix.LiveView.redirect(socket, to: "/users/log_in")}
|
|
|
|
|
|
|
|
user ->
|
|
|
|
{:cont,
|
|
|
|
Phoenix.LiveView.assign(
|
|
|
|
socket,
|
|
|
|
:current_user,
|
|
|
|
user
|
|
|
|
)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_mount(:live_user_not_allowed, _params, session, socket) do
|
|
|
|
case AshHqWeb.UserAuth.user_for_session_token(session["user_token"]) do
|
|
|
|
nil ->
|
|
|
|
{:cont,
|
|
|
|
Phoenix.LiveView.assign(
|
|
|
|
socket,
|
|
|
|
:current_user,
|
|
|
|
nil
|
|
|
|
)}
|
|
|
|
|
|
|
|
_user ->
|
|
|
|
{:halt,
|
|
|
|
Phoenix.LiveView.redirect(socket, to: Routes.app_view_path(AshHqWeb.Endpoint, :home))}
|
|
|
|
end
|
|
|
|
end
|
2022-08-07 11:22:58 +12:00
|
|
|
end
|