Fix Cloudflare IP geolocation not reaching LiveView on page load
The IPGeolocation plug was setting session data, but the live_session wasn't forwarding it to the LiveView mount. Also removed the root-path restriction so geolocation works on any initial page load.
This commit is contained in:
parent
b45aae070b
commit
6988116a42
3 changed files with 10 additions and 3 deletions
|
|
@ -11,7 +11,7 @@ defmodule AprsmeWeb.Plugs.IPGeolocation do
|
|||
def init(opts), do: opts
|
||||
|
||||
@impl true
|
||||
def call(%{request_path: "/", method: "GET"} = conn, _opts) do
|
||||
def call(%{method: "GET"} = conn, _opts) do
|
||||
conn
|
||||
|> get_session(:ip_geolocation)
|
||||
|> handle_geolocation(conn)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,11 @@ defmodule AprsmeWeb.Router do
|
|||
alias AprsmeWeb.Plugs.IPGeolocation
|
||||
alias AprsmeWeb.Plugs.RateLimiter
|
||||
|
||||
@doc false
|
||||
def regular_pages_session(conn) do
|
||||
%{"ip_geolocation" => Plug.Conn.get_session(conn, :ip_geolocation)}
|
||||
end
|
||||
|
||||
pipeline :browser do
|
||||
plug :accepts, ["html"]
|
||||
plug :fetch_session
|
||||
|
|
@ -96,6 +101,7 @@ defmodule AprsmeWeb.Router do
|
|||
pipe_through :browser
|
||||
|
||||
live_session :regular_pages,
|
||||
session: {__MODULE__, :regular_pages_session, []},
|
||||
on_mount: [{AprsmeWeb.UserAuth, :mount_current_user}, {AprsmeWeb.LocaleHook, :set_locale}] do
|
||||
live "/status", StatusLive.Index, :index
|
||||
live "/packets", PacketsLive.Index, :index
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ defmodule AprsmeWeb.Plugs.IPGeolocationTest do
|
|||
assert get_session(conn, :ip_geolocation) == nil
|
||||
end
|
||||
|
||||
test "skips non-root paths", %{conn: conn} do
|
||||
test "works on non-root paths", %{conn: conn} do
|
||||
conn =
|
||||
conn
|
||||
|> Map.put(:request_path, "/about")
|
||||
|
|
@ -56,7 +56,8 @@ defmodule AprsmeWeb.Plugs.IPGeolocationTest do
|
|||
|> put_req_header("cf-iplongitude", "-122.4194")
|
||||
|> IPGeolocation.call([])
|
||||
|
||||
assert get_session(conn, :ip_geolocation) == nil
|
||||
geo = get_session(conn, :ip_geolocation)
|
||||
assert geo == %{"lat" => 37.7749, "lng" => -122.4194}
|
||||
end
|
||||
|
||||
test "skips non-GET requests", %{conn: conn} do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue