Skip to content

Commit feb529a

Browse files
committed
fix: use raw paths for reset/register route definitions in sign_in_route
When `sign_in_route` is used inside a Phoenix router `scope`, the reset and register routes were double-nested. For example, inside `scope "/admin"`, a `reset_path: "/reset"` would generate a route at `/admin/admin/reset` instead of `/admin/reset`. The sign-in route itself was unaffected because it used the raw path option (`unquote(path)`) in the `live/4` call, letting the Phoenix scope mechanism add the prefix once. However, `reset_path` and `register_path` were passed through `Phoenix.Router.scoped_path/2` (which adds the scope prefix) and then used directly in `live/4` (which adds the scope prefix again). The fix maintains separate variables: the scoped paths are still used in session data (so the LiveView generates correct links), while the raw paths are used for route definitions. Fixes team-alembic/ash_authentication#1151
1 parent 6a10e69 commit feb529a

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

lib/ash_authentication_phoenix/router.ex

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,27 @@ defmodule AshAuthentication.Phoenix.Router do
351351
value -> Phoenix.Router.scoped_path(__MODULE__, value)
352352
end
353353

354+
register_route_path =
355+
case unquote(register_path) do
356+
nil -> nil
357+
{:unscoped, value} -> value
358+
value -> value
359+
end
360+
354361
reset_path =
355362
case unquote(reset_path) do
356363
nil -> nil
357364
{:unscoped, value} -> value
358365
value -> Phoenix.Router.scoped_path(__MODULE__, value)
359366
end
360367

368+
reset_route_path =
369+
case unquote(reset_path) do
370+
nil -> nil
371+
{:unscoped, value} -> value
372+
value -> value
373+
end
374+
361375
auth_routes_prefix =
362376
case unquote(auth_routes_prefix) do
363377
nil -> nil
@@ -396,11 +410,11 @@ defmodule AshAuthentication.Phoenix.Router do
396410
live(unquote(path), unquote(live_view), :sign_in, as: unquote(as))
397411

398412
if reset_path do
399-
live(reset_path, unquote(live_view), :reset, as: :"#{unquote(as)}_reset")
413+
live(reset_route_path, unquote(live_view), :reset, as: :"#{unquote(as)}_reset")
400414
end
401415

402416
if register_path do
403-
live(register_path, unquote(live_view), :register, as: :"#{unquote(as)}_register")
417+
live(register_route_path, unquote(live_view), :register, as: :"#{unquote(as)}_register")
404418
end
405419
end
406420
end

test/router_test.exs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ defmodule AshAuthentication.Phoenix.RouterTest do
3131
end
3232

3333
test "sign_in_routes respects the inherited router scope" do
34-
route =
35-
AshAuthentication.Phoenix.Test.Router
36-
|> Phoenix.Router.routes()
37-
|> Enum.find(&(&1.path == "/nested/sign-in"))
34+
routes = Phoenix.Router.routes(AshAuthentication.Phoenix.Test.Router)
35+
36+
route = Enum.find(routes, &(&1.path == "/nested/sign-in"))
3837

3938
{_, _, _, %{extra: %{session: session}}} = route.metadata.phoenix_live_view
4039

@@ -52,6 +51,12 @@ defmodule AshAuthentication.Phoenix.RouterTest do
5251
"resources" => nil
5352
}
5453
]}
54+
55+
assert Enum.find(routes, &(&1.path == "/nested/reset")),
56+
"reset route should be at /nested/reset, not /nested/nested/reset"
57+
58+
assert Enum.find(routes, &(&1.path == "/nested/register")),
59+
"register route should be at /nested/register, not /nested/nested/register"
5560
end
5661

5762
test "sign_in_routes respects unscoped" do

0 commit comments

Comments
 (0)