From b89170540baa98132645a126eaee70665a222d55 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 16 Feb 2025 11:06:56 -0500 Subject: [PATCH] improvement: add `LiveSession.opts/1` for compatibility --- .../live_session.ex | 51 ++++++++++++------- .../ash_authentication_phoenix.install.ex | 2 +- mix.exs | 2 +- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/lib/ash_authentication_phoenix/live_session.ex b/lib/ash_authentication_phoenix/live_session.ex index ff55cb86..6093a2fb 100644 --- a/lib/ash_authentication_phoenix/live_session.ex +++ b/lib/ash_authentication_phoenix/live_session.ex @@ -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 @@ -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. diff --git a/lib/mix/tasks/ash_authentication_phoenix.install.ex b/lib/mix/tasks/ash_authentication_phoenix.install.ex index d9f09367..908b8c3d 100644 --- a/lib/mix/tasks/ash_authentication_phoenix.install.ex +++ b/lib/mix/tasks/ash_authentication_phoenix.install.ex @@ -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 diff --git a/mix.exs b/mix.exs index 783d8359..3c26000e 100644 --- a/mix.exs +++ b/mix.exs @@ -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]},