Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routes not working with multiple user schemas #529

Open
isakemanuel opened this issue Jun 7, 2020 · 1 comment · May be fixed by #532
Open

Routes not working with multiple user schemas #529

isakemanuel opened this issue Jun 7, 2020 · 1 comment · May be fixed by #532

Comments

@isakemanuel
Copy link

I am trying to set up Pow with multiple user schemas. I followed the instructions here but I am unable to get it to work. I've tried to find a solution in the documentation but I didn't find a solution.

Using Elixir 1.10.3, Phoenix 1.5.1 and Pow 1.0.20.

The error I get in the browser is the following,

UndefinedFunctionError at GET /organizer/session/new
function HelloWeb.Router.Helpers.pow_session_path/3 is undefined or private

When compiling organizer_routes.ex, I get the warning warning: this clause cannot match because a previous clause at line 2 always matches. I am suspecting this is related to why the routes aren't working

Below are all the configuration changes I have made

# lib/hello_web/organizer_routes.ex
defmodule HelloWeb.Pow.OrganizerRoutes do
  use Pow.Phoenix.Routes
  
  def path_for(conn, verb, vars \\ [], query_params \\ []) do
    plug = Module.concat(["OrganizerPow", verb])
    Pow.Phoenix.Routes.path_for(conn, plug, vars, query_params)
  end
  
  def url_for(conn, verb, vars \\ [], query_params \\ []) do
    plug = Module.concat(["OrganizerPow", verb])
    Pow.Phoenix.Routes.url_for(conn, plug, vars, query_params)
  end
end
# lib/hello_web/router.ex
defmodule HelloWeb.Router do
  use HelloWeb, :router
  use Pow.Phoenix.Router

  # ...

  pipeline :pow_organizer do
    plug :set_pow_config,
      repo: Hello.Repo,
      user: Hello.Organizers.Organizer,
      current_user_assigns_key: :current_organizer,
      session_key: "organizer_auth",
      routes_backend: HelloWeb.Pow.OrganizerRoutes,
      plug: Pow.Plug.Session
  end

  defp set_pow_config(conn, config), do: Pow.Plug.put_config(conn, config)

  # I've temporarily disabled these routes since it seems Pow otherwise uses these for the organizer user schema as well
  # scope "/" do
    # pipe_through :browser
    # pow_routes()
  # end

  scope "/", HelloWeb do
    pipe_through :browser

    get "/", PageController, :index
  end


  scope "/organizer", as: :organizer do
    pipe_through [:browser, :pow_organizer]
    pow_routes()
  end

  # ...
end
# lib/hello_web/endpoint.ex
defmodule HelloWeb.Endpoint do
  use Phoenix.Endpoint, otp_app: :hello

  # ...
  
  plug Plug.Session, @session_options

  plug Pow.Plug.Session,
    repo: Hello.Repo,
    user: Hello.Users.Organizer,
    current_user_assigns_key: :current_organizer,
    session_key: "organizer_auth"

  plug Pow.Plug.Session,
    user: Hello.Users.User,
    repo: Hello.Repo,
    current_user_assigns_key: :current_user,
    session_key: "user_auth"

  plug HelloWeb.Router
end
@isakemanuel
Copy link
Author

Update: I was able to resolve this issue after digging through the source code.

Using the below code worked. The difference is the arity of the functions and the number of arguments passed to the functions in Pow.Phoenix.Routes

defmodule HelloWeb.Pow.OrganizerRoutes do
  use Pow.Phoenix.Routes
  
  def path_for(conn, plug, verb, vars \\ [], query_params \\ []) do
    plug = Module.concat(["OrganizerPow", plug])
    Pow.Phoenix.Routes.path_for(conn, plug, verb, vars, query_params)
  end
  
  def url_for(conn, plug, verb, vars \\ [], query_params \\ []) do
    plug = Module.concat(["OrganizerPow", plug])
    Pow.Phoenix.Routes.url_for(conn, plug, verb, vars, query_params)
  end
end

@isakemanuel isakemanuel linked a pull request Jun 8, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant