docs: improve examples (#308)

This commit is contained in:
Arthur Clemens 2023-10-16 05:21:03 +02:00 committed by GitHub
parent 6c1bf4bbd7
commit 38da09af84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,19 +8,21 @@ ash_authentication_live_session :session_name do
end end
``` ```
# LiveUserAuth
There are two problems with the above, however. There are two problems with the above, however.
1. If there is no user present, it will not set `current_user: nil`. 1. If there is no user present, it will not set `current_user: nil`.
2. You may want a way to require that a user is present for some routes, and not for others. 2. You may want a way to require that a user is present for some routes, and not for others.
## Authentication helper
To accomplish this, we use standard Phoenix [`on_mount` hooks](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#on_mount/1-examples). Lets define a hook that gives us three potential behaviors, one for optionally having a user signed in, one for requiring a signed in user, and one for requiring that there is no signed in user. To accomplish this, we use standard Phoenix [`on_mount` hooks](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#on_mount/1-examples). Lets define a hook that gives us three potential behaviors, one for optionally having a user signed in, one for requiring a signed in user, and one for requiring that there is no signed in user.
```elixir ```elixir
# lib/my_app_web/live_user_auth.ex
defmodule MyAppWeb.LiveUserAuth do defmodule MyAppWeb.LiveUserAuth do
@moduledoc """ @moduledoc """
Helpers for authenticating users in liveviews Helpers for authenticating users in LiveViews.
""" """
import Phoenix.Component import Phoenix.Component
@ -55,15 +57,21 @@ end
And we can use this as follows: And we can use this as follows:
```elixir ```elixir
ash_authentication_live_session :authentication_required, # lib/my_app_web/router.ex
on_mount: {MyAppWeb.LiveUserAuth, :live_user_required} do # ...
live "/protected_route", ProjectLive.Index, :index scope "/", MyAppWeb do
end # ...
ash_authentication_live_session :authentication_required,
on_mount: {MyAppWeb.LiveUserAuth, :live_user_required} do
live "/protected_route", ProjectLive.Index, :index
end
ash_authentication_live_session :authentication_optional, ash_authentication_live_session :authentication_optional,
on_mount: {MyAppWeb.LiveUserAuth, :live_user_optional} do on_mount: {MyAppWeb.LiveUserAuth, :live_user_optional} do
live "/", ProjectLive.Index, :index live "/", ProjectLive.Index, :index
end end
end
# ...
``` ```
You can also use this to prevent users from visiting the auto generated `sign_in` route: You can also use this to prevent users from visiting the auto generated `sign_in` route: