From 870964155c3e231c03d49351f021d36ae3c53951 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Thu, 12 Sep 2024 10:47:46 -0400 Subject: [PATCH] fix: apply `auth_routes_prefix` logic to `reset_route` as well --- dev/dev_web/router.ex | 2 +- documentation/tutorials/get-started.md | 4 ++-- .../components/password/reset_form.ex | 10 ++++++++- .../components/reset/form.ex | 10 ++++++++- lib/ash_authentication_phoenix/reset_live.ex | 2 ++ lib/ash_authentication_phoenix/router.ex | 22 ++++++++++++++++--- 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/dev/dev_web/router.ex b/dev/dev_web/router.ex index 3fc17da..479bd64 100644 --- a/dev/dev_web/router.ex +++ b/dev/dev_web/router.ex @@ -31,7 +31,7 @@ defmodule DevWeb.Router do pipe_through :browser sign_out_route(AuthController, "/sign-out") - reset_route() + reset_route(auth_routes_prefix: "/auth") sign_in_route( path: "/sign-in", diff --git a/documentation/tutorials/get-started.md b/documentation/tutorials/get-started.md index 7a893ae..f0d771e 100644 --- a/documentation/tutorials/get-started.md +++ b/documentation/tutorials/get-started.md @@ -377,7 +377,7 @@ defmodule ExampleWeb.Router do # Leave out `register_path` and `reset_path` if you don't want to support # user registration and/or password resets respectively. sign_in_route(register_path: "/register", reset_path: "/reset", auth_routes_prefix: "/auth") - reset_route [] + reset_route [auth_routes_prefix: "/auth"] # <-- add these lines end @@ -431,7 +431,7 @@ Liveviews provided by AshAuthentication.Phoenix will use the same root layout co If you would like to use your own layout file instead, you can specify this as an option to the route helpers, eg. ```elixir -reset_route(layout: {MyAppWeb, :live}) +reset_route(layout: {MyAppWeb, :live}, auth_routes_prefix: "/auth") ``` ## AuthController diff --git a/lib/ash_authentication_phoenix/components/password/reset_form.ex b/lib/ash_authentication_phoenix/components/password/reset_form.ex index 401e89a..53fad2e 100644 --- a/lib/ash_authentication_phoenix/components/password/reset_form.ex +++ b/lib/ash_authentication_phoenix/components/password/reset_form.ex @@ -89,7 +89,15 @@ defmodule AshAuthentication.Phoenix.Components.Password.ResetForm do phx-submit="submit" phx-change="change" phx-target={@myself} - action={auth_path(@socket, @subject_name, @auth_routes_prefix, @strategy, :reset_request)} + action={ + auth_path( + @socket, + @subject_name, + @auth_routes_prefix, + @strategy, + :reset_request + ) + } method="POST" class={override_for(@overrides, :form_class)} > diff --git a/lib/ash_authentication_phoenix/components/reset/form.ex b/lib/ash_authentication_phoenix/components/reset/form.ex index 760d5fc..870351f 100644 --- a/lib/ash_authentication_phoenix/components/reset/form.ex +++ b/lib/ash_authentication_phoenix/components/reset/form.ex @@ -105,7 +105,15 @@ defmodule AshAuthentication.Phoenix.Components.Reset.Form do phx-submit="submit" phx-trigger-action={@trigger_action} phx-target={@myself} - action={auth_path(@socket, @subject_name, @auth_routes_prefix, @strategy, :reset)} + action={ + auth_path( + @socket, + @subject_name, + @auth_routes_prefix, + @strategy, + :reset + ) + } method="POST" class={override_for(@overrides, :form_class)} > diff --git a/lib/ash_authentication_phoenix/reset_live.ex b/lib/ash_authentication_phoenix/reset_live.ex index 681bfa2..f06889e 100644 --- a/lib/ash_authentication_phoenix/reset_live.ex +++ b/lib/ash_authentication_phoenix/reset_live.ex @@ -33,6 +33,7 @@ defmodule AshAuthentication.Phoenix.ResetLive do |> assign_new(:otp_app, fn -> nil end) |> assign(:current_tenant, session["tenant"]) |> assign(:context, session["context"] || %{}) + |> assign(:auth_routes_prefix, session["auth_routes_prefix"]) {:ok, socket} end @@ -55,6 +56,7 @@ defmodule AshAuthentication.Phoenix.ResetLive do otp_app={@otp_app} id={override_for(@overrides, :reset_id, "reset")} token={@token} + auth_routes_prefix={@auth_routes_prefix} overrides={@overrides} current_tenant={@current_tenant} context={@context} diff --git a/lib/ash_authentication_phoenix/router.ex b/lib/ash_authentication_phoenix/router.ex index 318ee9b..75c68ad 100644 --- a/lib/ash_authentication_phoenix/router.ex +++ b/lib/ash_authentication_phoenix/router.ex @@ -26,10 +26,10 @@ defmodule AshAuthentication.Phoenix.Router do scope "/", MyAppWeb do pipe_through :browser - sign_in_route + sign_in_route auth_routes_prefix: "/auth" sign_out_route AuthController auth_routes_for MyApp.Accounts.User, to: AuthController - reset_route + reset_route auth_routes_prefix: "/auth" end ``` """ @@ -136,6 +136,8 @@ defmodule AshAuthentication.Phoenix.Router do If you are using any of the components provided by `AshAuthenticationPhoenix`, you will need to supply them with the `auth_routes_prefix` assign, set to the `path` you provide here (set to `/auth` by default). + You also will need to set `auth_routes_prefix` on the `reset_route`, i.e `reset_route(auth_routes_prefix: "/auth")` + ## Options * `path` - the path to mount auth routes at. Defaults to `/auth`. If changed, you will also want @@ -360,6 +362,7 @@ defmodule AshAuthentication.Phoenix.Router do {otp_app, opts} = Keyword.pop(opts, :otp_app) {layout, opts} = Keyword.pop(opts, :layout) {on_mount, opts} = Keyword.pop(opts, :on_mount) + {auth_routes_prefix, opts} = Keyword.pop(opts, :auth_routes_prefix) {overrides, opts} = Keyword.pop(opts, :overrides, [AshAuthentication.Phoenix.Overrides.Default]) @@ -369,6 +372,13 @@ defmodule AshAuthentication.Phoenix.Router do |> Keyword.put_new(:alias, false) quote do + auth_routes_prefix = + case unquote(auth_routes_prefix) do + nil -> nil + {:unscoped, value} -> value + value -> Phoenix.Router.scoped_path(__MODULE__, value) + end + scope unquote(path), unquote(opts) do import Phoenix.LiveView.Router, only: [live: 4, live_session: 3] @@ -385,7 +395,13 @@ defmodule AshAuthentication.Phoenix.Router do live_session_opts = [ session: {AshAuthentication.Phoenix.Router, :generate_session, - [%{"overrides" => unquote(overrides), "otp_app" => unquote(otp_app)}]}, + [ + %{ + "auth_routes_prefix" => auth_routes_prefix, + "overrides" => unquote(overrides), + "otp_app" => unquote(otp_app) + } + ]}, on_mount: on_mount ]