require authenticated user

Hi,Im still new to elixir and phoenix. I'm using ash_authentication_phoenix. i want to redirect user after sign-in to "/dashboard" and prevent unsigned user from the route. I read the doc but I cant figure it.
14 Replies
ZachDaniel
ZachDaniel•3y ago
in ash_hq we use this:
defmodule AshHqWeb.LiveUserAuth do
@moduledoc """
Helpers for authenticating users in liveviews
"""

import Phoenix.Component
use AshHqWeb, :verified_routes

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
end
defmodule AshHqWeb.LiveUserAuth do
@moduledoc """
Helpers for authenticating users in liveviews
"""

import Phoenix.Component
use AshHqWeb, :verified_routes

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
end
And then we do this kind of thing:
ash_authentication_live_session :authenticated_only,
on_mount: [
{AshHqWeb.InitAssigns, :default},
{AshHqWeb.LiveUserAuth, :live_user_required}
],
session: {AshAuthentication.Phoenix.LiveSession, :generate_session, []},
root_layout: {AshHqWeb.LayoutView, :root} do
live("/users/settings", AppViewLive, :user_settings)
end
ash_authentication_live_session :authenticated_only,
on_mount: [
{AshHqWeb.InitAssigns, :default},
{AshHqWeb.LiveUserAuth, :live_user_required}
],
session: {AshAuthentication.Phoenix.LiveSession, :generate_session, []},
root_layout: {AshHqWeb.LayoutView, :root} do
live("/users/settings", AppViewLive, :user_settings)
end
This is what you'd do for liveviews For regular views/controllers you'd use a plug.
icecarrotbear
icecarrotbearOP•3y ago
Thanks you so much.it work..I did not aware ash-hq web on github.
ZachDaniel
ZachDaniel•3y ago
Yep! That can be a great place to look for example code.
brittonjb
brittonjb•3y ago
As an aside here Zach, might be worth compiling a few example repos into a guide on the Ash primary repo docs folder, between real world, twitter, and ash hq you can usually find what you need.
ZachDaniel
ZachDaniel•3y ago
Good point. We have a link to repos (not including realworld because that is new) in the Media page, but adding them to the sidebar would be cool
dungnguyen8134
dungnguyen8134•3y ago
I copy code from ash_hq and it doesn't load current user
live_session :authenticated_only,
on_mount: [
{OrangeCmsWeb.LiveUserAuth, :live_user_required}
],
session: {AshAuthentication.Phoenix.LiveSession, :generate_session, []} do

scope "/" do
live "/p", ProjectLive.Index, :index
end
end
live_session :authenticated_only,
on_mount: [
{OrangeCmsWeb.LiveUserAuth, :live_user_required}
],
session: {AshAuthentication.Phoenix.LiveSession, :generate_session, []} do

scope "/" do
live "/p", ProjectLive.Index, :index
end
end
then I replace live_session with ash_authentication_live_session and I got this compile error
== Compilation error in file lib/orange_cms_web/router.ex ==
** (ArgumentError) invalid live_session :session

expected a map with string keys or an MFA tuple, got [{AshAuthentication.Phoenix.LiveSession, :generate_session, [nil]}, {AshAuthentication.Phoenix.LiveSession, :generate_session, []}]

(phoenix_live_view 0.18.16) lib/phoenix_live_view/router.ex:255: anonymous fn/3 in Phoenix.LiveView.Router.validate_live_session_opts/3
(elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
(phoenix_live_view 0.18.16) lib/phoenix_live_view/router.ex:226: Phoenix.LiveView.Router.__live_session__/3
lib/orange_cms_web/router.ex:52: (module)
(elixir 1.14.3) lib/kernel/parallel_compiler.ex:340: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
== Compilation error in file lib/orange_cms_web/router.ex ==
** (ArgumentError) invalid live_session :session

expected a map with string keys or an MFA tuple, got [{AshAuthentication.Phoenix.LiveSession, :generate_session, [nil]}, {AshAuthentication.Phoenix.LiveSession, :generate_session, []}]

(phoenix_live_view 0.18.16) lib/phoenix_live_view/router.ex:255: anonymous fn/3 in Phoenix.LiveView.Router.validate_live_session_opts/3
(elixir 1.14.3) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
(phoenix_live_view 0.18.16) lib/phoenix_live_view/router.ex:226: Phoenix.LiveView.Router.__live_session__/3
lib/orange_cms_web/router.ex:52: (module)
(elixir 1.14.3) lib/kernel/parallel_compiler.ex:340: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
finally I remove the :session options and it works
ash_authentication_live_session :authenticated_only,
on_mount: [
{OrangeCmsWeb.LiveUserAuth, :live_user_required}
] do
scope "/" do
live "/p", ProjectLive.Index, :index
end
end
ash_authentication_live_session :authenticated_only,
on_mount: [
{OrangeCmsWeb.LiveUserAuth, :live_user_required}
] do
scope "/" do
live "/p", ProjectLive.Index, :index
end
end
ZachDaniel
ZachDaniel•3y ago
🤔 Ah, yeah, that is a bug but your solution is correct There apparently has to be only one session hook
dungnguyen8134
dungnguyen8134•3y ago
I think we should add this to AshAuthentication.Phoenix tutorial
ZachDaniel
ZachDaniel•3y ago
Does it not show using ash_authentication_live_session in that tutorial? Can you show me where its missing? Happy to fix
dungnguyen8134
dungnguyen8134•3y ago
No I cannot find in the tutorial
ZachDaniel
ZachDaniel•3y ago
I will add a liveview specific tutorial and link to it from the end of the getting started guide 😄
ZachDaniel
ZachDaniel•3y ago
GitHub
ash_authentication_phoenix/use-ash-authentication-with-liveview.md ...
Drop-in authentication support for Phoenix apps using AshAuthentication. - ash_authentication_phoenix/use-ash-authentication-with-liveview.md at main · team-alembic/ash_authentication_phoenix
ZachDaniel
ZachDaniel•3y ago
This will be on ash HQ in the next release
dungnguyen8134
dungnguyen8134•3y ago
That's great, thank you

Did you find this page helpful?