smpallen99 / coherence

Coherence is a full featured, configurable authentication system for Phoenix
MIT License
1.27k stars 223 forks source link

function Router.Helpers.session_path/2 is undefined #295

Closed axelclark closed 6 years ago

axelclark commented 6 years ago

I'm attempting to update my coherence version from 0.3 to 0.5. I've followed the update instructions to 0.4, but I'm getting the following error:

     ** (UndefinedFunctionError) function Router.Helpers.session_path/2 is undefined (module Router.Helpers is not available)
     code: conn = get conn, fantasy_league_path(conn, :show, league.id)
     stacktrace:
       Router.Helpers.session_path(%Plug.Conn{adapter: {Plug.Adapters.Test.Conn, :...}, assigns: %{}, before_send: [#Function<0.96292254/1 in Plug.CSRFProtection.call/2>, #Function<4.90664251/1 in Phoenix.Controller.fetch_flash/2>, #Function<0.112984571/1 in Plug.Session.before_send/2>, #Function<1.104928615/1 in Plug.Logger.call/2>], body_params: %{}, cookies: %{}, halted: false, host: "www.example.com", method: "GET", owner: #PID<0.2403.0>, params: %{"id" => "103504"}, path_info: ["fantasy_leagues", "103504"], path_params: %{"id" => "103504"}, peer: {{127, 0, 0, 1}, 111317}, port: 80, private: %{Ex338.Router => {[], %{}}, :phoenix_endpoint => Ex338.Endpoint, :phoenix_flash => %{}, :phoenix_format => "html", :phoenix_pipelines => [:protected], :phoenix_recycled => true, :phoenix_router => Ex338.Router, :plug_session => %{"user_return_to" => "/fantasy_leagues/103504"}, :plug_session_fetch => :done, :plug_session_info => :write, :plug_skip_csrf_protection => true}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %{}, req_headers: [], request_path: "/fantasy_leagues/103504", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "4jc5cq1j1q0ahn17c6ehkcflbam0tkfk"}, {"x-frame-options", "SAMEORIGIN"}, {"x-xss-protection", "1; mode=block"}, {"x-content-type-options", "nosniff"}], scheme: :http, script_name: [], secret_key_base: "rMId5sSgp3+wKTXMCXXl38I/lxPO8AWSF9PFKhmqj4N1cJyK5NmZn3QgqLT2NQd8", state: :unset, status: nil}, :new)
       (coherence) lib/coherence/plugs/authorization/session.ex:244: Coherence.Authentication.Session.assert_login/3
       (ex338) web/router.ex:16: Ex338.Router.protected/2
       (ex338) web/router.ex:1: anonymous fn/1 in Ex338.Router.__match_route__/4
       (phoenix) lib/phoenix/router.ex:273: Phoenix.Router.__call__/1
       (ex338) web/router.ex:5: Ex338.Router.call/2
       (ex338) lib/ex338/endpoint.ex:1: Ex338.Endpoint.plug_builder_call/2
       (ex338) lib/ex338/endpoint.ex:1: Ex338.Endpoint.call/2
       (phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
       test/controllers/fantasy_league_controller_test.exs:14: (test)

I've looked through the diff after I generated the new boilerplate and I can't find anything that I think would cause the error. I'm using Phoenix 1.3, but I haven't transitioned all of my files/modules to the new naming convention and folder structure.

Any idea what the issue is? Thanks!

Kukunin commented 6 years ago

I encountered the same problem. In my case it meant that I didn't have configured routes properly. There is a section in the README:

# lib/my_project_web/router.ex

defmodule MyProjectWeb.Router do
  use MyProjectWeb, :router
  use Coherence.Router         # Add this

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug Coherence.Authentication.Session  # Add this
  end

  pipeline :protected do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
    plug Coherence.Authentication.Session, protected: true
  end

  # Add this block
  scope "/" do
    pipe_through :browser
    coherence_routes()
  end

  # Add this block
  scope "/" do
    pipe_through :protected
    coherence_routes :protected
  end

  scope "/", MyProjectWeb do
    pipe_through :browser

    get "/", PageController, :index
    # add public resources below
  end

  scope "/", MyProjectWeb do
    pipe_through :protected

    # add protected resources below
    resources "/privates", MyProjectWeb.PrivateController
  end
end
axelclark commented 6 years ago

@Kukunin Thanks! I checked and my router appears to be correct.

However, I discovered I needed to add a web_module to my :coherence config. The function below wasn't returning anything for Config.web_module.

 # lib/coherence/plugs/authorization/utils.ex
  def new_session_path(conn) do
    Module.concat(Config.web_module, Router.Helpers).session_path(conn, :new)
  end