Newbie question about module imports

I just started learning Phoenix and I’m trying to work on authentication Plug.
So far I have the following

defmodule Learnp.Plugs.RequireAuthentication do
  import Plug.Conn
  import Phoenix.Controller

  def init(options) do
    options
  end

  def call(conn, _) do
    user = get_session(conn, :current_user)
    case user do
      nil ->
        conn |> redirect(to: "/sign_in") |> halt
      _ ->
        conn |> assign(:current_user, user)
    end
  end

end

But if I try to import import Learnp.Router.Helpers and replace the "sign_in" path with the named route helper I get the error module Learnp.Router.Helpers is not loaded and could not be found.

Is it wrong to import the router helpers on places other than views?
The very messy code is here: https://github.com/cpgo/learnp/tree/dev
:smile:

1 Like

You can quickly end up with circular compilation dependencies. The path helpers do not exist until the router is compiled. However the router cannot be compiled until all plugs used in the router are compiled. If one of those plugs tries to import the path helpers, you can have a circular compilation dependency.

2 Likes

How would you implement this? Just leave the hard coded path as it its right now?
I noted that if I use the plug directly on the controller and not on a pipeline I can do it with the helpers.