Skip to content

Commit

Permalink
improvement: add LiveSession.opts/1 for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Feb 16, 2025
1 parent 26ea611 commit b891705
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 20 deletions.
51 changes: 33 additions & 18 deletions lib/ash_authentication_phoenix/live_session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,8 @@ defmodule AshAuthentication.Phoenix.LiveSession do
end

quote generated: true do
on_mount = [LiveSession]

opts = unquote(opts)

session = {LiveSession, :generate_session, [opts[:otp_app], List.wrap(opts[:session])]}

opts =
opts
|> Keyword.update(:on_mount, on_mount, &(on_mount ++ List.wrap(&1)))
|> Keyword.put(:session, session)

{otp_app, opts} = Keyword.pop(opts, :otp_app)

opts =
if otp_app do
Keyword.update!(opts, :on_mount, &[{LiveSession, {:set_otp_app, otp_app}} | &1])
else
opts
end
opts = LiveSession.opts(opts)

require Phoenix.LiveView.Router

Expand All @@ -79,6 +62,38 @@ defmodule AshAuthentication.Phoenix.LiveSession do

defp expand_alias(other, _env), do: other

@doc """
Get options that should be passed to `live_session`.
This is useful for integrating with other tools that require a custom `live_session`,
like `beacon_live_admin`. For example:
```elixir
beacon_live_admin AshAuthentication.Phoenix.LiveSession.opts(beacon: :opts) do
...
end
```
"""
def opts(custom_opts \\ []) do
on_mount = [LiveSession]

session =
{__MODULE__, :generate_session, [custom_opts[:otp_app], List.wrap(custom_opts[:session])]}

opts =
custom_opts
|> Keyword.update(:on_mount, on_mount, &(on_mount ++ List.wrap(&1)))
|> Keyword.put(:session, session)

{otp_app, opts} = Keyword.pop(opts, :otp_app)

if otp_app do
Keyword.update!(opts, :on_mount, &[{LiveSession, {:set_otp_app, otp_app}} | &1])
else
opts
end
end

@doc """
Inspects the incoming session for any subject_name -> subject values and loads
them into the socket's assigns.
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/ash_authentication_phoenix.install.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ if Code.ensure_loaded?(Igniter) do
|> module_option(:token)

install? =
!Igniter.Project.Deps.get_dependency_declaration(igniter, :ash_authentication)
!match?({:ok, _}, Igniter.Project.Deps.get_dep(igniter, :ash_authentication))

igniter =
if install? do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ defmodule AshAuthentication.Phoenix.MixProject do
{:bcrypt_elixir, "~> 3.0"},
{:slugify, "~> 1.3"},
{:gettext, "~> 0.26", optional: true},
{:igniter, "~> 0.5 and >= 0.5.1", optional: true},
{:igniter, "~> 0.5 and >= 0.5.25", optional: true},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false},
{:dialyxir, "~> 1.2", only: [:dev, :test], runtime: false},
{:doctor, "~> 0.18", only: [:dev, :test]},
Expand Down

0 comments on commit b891705

Please sign in to comment.