i'm trying write plug
authenticate users (checking session actually). have trouble getting redirect route. think because route generation happens after plug activated.
anyway got error this: undefined function testapp.page_path/2
in regular context page_path/2
exists , works.
defmodule testapp.plugs.authenticate import plug.conn def init(default), do: default def call(conn, _) user = plug.conn.get_session(conn, :current_user) if not is_nil(user) assign(conn, :user, user) else conn |> phoenix.controller.put_flash(:warning, "user not authenticated.") |> phoenix.controller.redirect(to: testapp.page_path(conn, :index)) |> halt end end end
router helpers included in controllers , view via web.ex
file:
def controller quote use phoenix.controller ... import myapp.router.helpers end end def view quote use phoenix.view, root: "web/templates" ... import myapp.router.helpers ... end end
as can see, both controller
, view
functions import myapp.router.helpers
module. helper (_path
, url
) functions defined.
you can either use qualified name:
phoenix.controller.redirect(to: testapprouter.helpers.page_path(conn, :index))
or can import route helpers , use page_path
import myapp.router.helpers # or import myapp.router.helpers, only: [page_path: 2]
however, if use plug in pipeline in router, cause circular dependency , code not compile.
Comments
Post a Comment