ericdude4/shopifex

Session not persisting between requests

arialblack14 opened this issue · 2 comments

Well @ericdude4 , I must be doing something wrong.
When I click on a link from the initial page embedded in the app, the session does not persist.

Here is what I have in my router.ex

scope "/auth", MyAppWeb do
    pipe_through [:browser, :shopify_entrypoint]

    get "/", AuthController, :auth
    get "/install", AuthController, :install
  end

  scope "/", MyAppWeb do
    pipe_through [:browser, :shopify_session]

    get "/", EventController, :index
    resources "/events", EventController, 
  end

Here is the initial response, from get '/auth'

[info] GET /auth
[debug] Processing with MyAppWeb.AuthController.auth/2
  Parameters: %{"hmac" => "ca98fc37c42fd5872397600e3dwerrf1c53556dab9f1b98d5f93e598162254", "locale" => "en", "session" => "54798671d0a9f8c3a59ereettc0cafd6c5438124bcfe8748107257f6198bfb04", "shop" => "someshop.myshopify.com", "timestamp" => "1590846273"}
  Pipelines: [:browser, :shopify_entrypoint]
--------------------[conn from Shopifex.Plug.ShopifyEntrypoint]-----------------------------------------
%Plug.Conn{
  adapter: {Plug.Cowboy.Conn, :...},
  assigns: %{},
  before_send: [#Function<0.78923111/1 in Plug.CSRFProtection.call/2>,
   #Function<2.86443083/1 in Phoenix.Controller.fetch_flash/2>,
   #Function<0.55231287/1 in Plug.Session.before_send/2>,
   #Function<0.93523768/1 in Plug.Telemetry.call/2>,
   #Function<0.2186795/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>],
  body_params: %{},
  cookies: %{
    "_my_app_key" => "SFMyNTY.g3QAAAAA.ZmPHZTgugy2ORhhcf64qgRxfZdx8VyP-vzdrgvA"
  },
  halted: false,
  host: "feabaaa8c1bb.ngrok.io",
  method: "GET",
  owner: #PID<0.615.0>,
  params: %{
    "hmac" => "ca98fc37c42fd5872397600e3df9226121c53556dab9f1b98d5f93e598162254",
    "locale" => "en",
    "session" => "54798671d0a9f8c3a5945b638c0cafd6c5438124bcfe8748107257f6198bfb04",
    "shop" => "someshop.myshopify.com",
    "timestamp" => "1590846273"
  },
  path_info: ["auth"],
  path_params: %{},
  port: 80,
  private: %{
    AutomatoWeb.Router => {[], %{}},
    :phoenix_endpoint => MyAppWeb.Endpoint,
    :phoenix_flash => %{},
    :phoenix_format => "html",
    :phoenix_request_logger => {"request_logger", "request_logger"},
    :phoenix_router => MyAppWeb.Router,
    :plug_session => %{},
    :plug_session_fetch => :done
  },
  query_params: %{
    "hmac" => "ca98fc37c42fd5872397600e3df9226121c53556dab9f1b98d5f93e598162254",
    "locale" => "en",
    "session" => "54798671d0a9f8c3a5945b638c0cafd6c5438124bcfe8748107257f6198bfb04",
    "shop" => "someshop.myshopify.com",
    "timestamp" => "1590846273"
  },
  query_string: "hmac=ca98fc37c42fd5872397600e3df9226121c53556dab9f1brgergwergeg62254&locale=en&session=54798671d0a9f8c3a5945b638c0cafd6c5438124bcfe8748107257f6198bfb04&shop=someshop.myshopify.com&timestamp=1590846273",
  remote_ip: {127, 0, 0, 1},
  req_cookies: %{
    "_my_app_key" => "SFMyNTY.g3QAAAAA.ZmPHZTi7P6CkiR2ORhhcf64qgRxfZdx8VyP-vzdrgvA"
  },
  req_headers: [
    {"accept",
     "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"},
    {"accept-encoding", "gzip, deflate, br"},
    {"accept-language", "en-US,en;q=0.9,el;q=0.8,la;q=0.7"},
    {"cookie",
     "_automato_key=SFMyNTY.g3QAAAAA.ZmPHZTi7P6CkiR2ORhhcf64qgRxfZdx8VyP-vzdrgvA"},
    {"dnt", "1"},
    {"host", "feaegrc1bb.ngrok.io"},
    {"sec-fetch-dest", "iframe"},
    {"sec-fetch-mode", "navigate"},
    {"sec-fetch-site", "cross-site"},
    {"upgrade-insecure-requests", "1"},
    {"user-agent",
     "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"},
    {"x-forwarded-for", "109.242.21.16"},
    {"x-forwarded-proto", "https"}
  ],
  request_path: "/auth",
  resp_body: nil,
  resp_cookies: %{},
  resp_headers: [
    {"cache-control", "max-age=0, private, must-revalidate"},
    {"x-request-id", "FhPSQmGujQPJ8MUAAADh"},
    {"x-xss-protection", "1; mode=block"},
    {"x-content-type-options", "nosniff"},
    {"x-download-options", "noopen"},
    {"x-permitted-cross-domain-policies", "none"},
    {"cross-origin-window-policy", "deny"}
  ],
  scheme: :http,
  script_name: [],
  secret_key_base: :...,
  state: :unset,
  status: nil
}
-------------------------------------------------------------
[debug] QUERY OK source="shops" db=16.6ms queue=1.5ms idle=598.7ms
SELECT s0."id", s0."access_token", s0."scopes", s0."url", s0."inserted_at", s0."updated_at" FROM "shops" AS s0 WHERE (s0."url" = $1) ["someshop.myshopify.com"]
[info] Sent 302 in 110ms
[info] GET /
[debug] Processing with MyAppWeb.EventController.index/2
  Parameters: %{}
  Pipelines: [:browser, :shopify_session]
******************Result of Phoenix.Controller.get_flash(conn, :shop) in Shopifex.Plug.ShopifySession .call***************
%MyAppWeb.ShopifyApp.Shop{
  __meta__: #Ecto.Schema.Metadata<:loaded, "shops">,
  access_token: "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",
  id: 8,
  inserted_at: ~N[2020-05-30 13:33:27],
  scopes: "write_inventory,write_products,read_orders",
  updated_at: ~N[2020-05-30 13:33:27],
  url: "someshop.myshopify.com"
}
[debug] QUERY OK source="events" db=1.7ms queue=1.1ms idle=1056.8ms
SELECT e0."id", e0."data", e0."topic", e0."inserted_at", e0."updated_at" FROM "events" AS e0 []
[info] Sent 200 in 38ms

And here is a subsequent request, which again gets redirected to /auth

[info] GET /events/new
[debug] Processing with MyAppWeb.EventController.new/2
  Parameters: %{}
  Pipelines: [:browser, :shopify_session]
****************Result of Phoenix.Controller.get_flash(conn, :shop) in Shopifex.Plug.ShopifySession .call*****************
nil
[info] Sent 302 in 656µs
[info] GET /auth
[debug] Processing with MyAppWeb.AuthController.auth/2
  Parameters: %{}
  Pipelines: [:browser, :shopify_entrypoint]
[info] Sent 200 in 447µs

Any help/info would be much appreciated.

Hi @arialblack14, Thanks for being my guinea pig for this package :)

I have resolved the issue, and updated the documentation. For you to fix this, simply change your :browser pipeline in router.ex to look like this:

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug Shopifex.Plug.FetchFlash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug Shopifex.Plug.LoadInIframe
  end 

Then run mix deps.update shopifex

Sorry about my lack of testing beforehand. I was able to get it working smoothly with this recent change. Thank you for your bug report.

Haha, glad I could help :)
Works great now! 🤘 🤘