chore(AshAuthenticationPhoenix): added library and updated ressources testing password strategy
This commit is contained in:
parent
f154eea055
commit
192ceaed45
24 changed files with 682 additions and 25 deletions
44
lib/mv_web/live_user_auth.ex
Normal file
44
lib/mv_web/live_user_auth.ex
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
defmodule MvWeb.LiveUserAuth do
|
||||
@moduledoc """
|
||||
Helpers for authenticating users in LiveViews.
|
||||
"""
|
||||
|
||||
import Phoenix.Component
|
||||
use MvWeb, :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 {MvWeb.LiveUserAuth, :current_user}
|
||||
def on_mount(:current_user, _params, session, socket) do
|
||||
return_to = session[:return_to]
|
||||
socket =
|
||||
socket
|
||||
|> assign(:return_to, return_to)
|
||||
|> AshAuthentication.Phoenix.LiveSession.assign_new_resources(session)
|
||||
{:cont, session, socket}
|
||||
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
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue