Request_path and redirects

So I have a plug that checks if you are authed before you go about your business.

  def authenticated?(conn, _opts) do
   if conn.assigns.current_user do
     conn
   else
     conn
     |> put_flash(:error, "You must be logged in to access that page")
     |> redirect(to: Helpers.session_path(conn, :new))
     |> halt()
   end
  end

The conn has a request_path of say “/foo/bar”

Whats the best way to later look up that request_path after I did the redirect so that way I can send whom ever back on the correct path after they have logged in?

I guess I could shove it in a session var? and look it up later to see if there was one.

I just also had another thought, would it be smarter to pass the request_path as a string query param when doing the redirect? Is there anything I should be worried about doing it that way?

I’ve seen two general machisms to temporarily store the origin-path:

  • Append a query parameter with the origin path (Helpers.session_path(conn, :new, origin_path: conn.request_path) or something along that lines)
  • Storing the origin path in a cookie. Often causes problems when multiple pathes are accessed simultaniously, eg. after a browser restart
2 Likes