The plug was incorrectly placed in the endpoint, causing it to run on all requests including APIs. Moved to :browser pipeline where it belongs, and removed redundant fetch_session call since the pipeline handles it.
25 lines
698 B
Elixir
25 lines
698 B
Elixir
defmodule ToweropsWeb.Plugs.CaptureTimezone do
|
|
@moduledoc """
|
|
Captures the cf-timezone header from Cloudflare and stores it in session.
|
|
|
|
Cloudflare provides timezone detection via the cf-timezone header using
|
|
IANA timezone database format (e.g., "America/Chicago", "Europe/London").
|
|
|
|
Falls back to "UTC" if the header is not present (development, non-Cloudflare).
|
|
"""
|
|
import Plug.Conn
|
|
|
|
@doc false
|
|
def init(opts), do: opts
|
|
|
|
@doc false
|
|
def call(conn, _opts) do
|
|
timezone =
|
|
case get_req_header(conn, "cf-timezone") do
|
|
[tz] when is_binary(tz) and byte_size(tz) > 0 -> tz
|
|
_ -> "UTC"
|
|
end
|
|
|
|
put_session(conn, :detected_timezone, timezone)
|
|
end
|
|
end
|