ueberauth / ueberauth

An Elixir Authentication System for Plug-based Web Applications
MIT License
1.62k stars 120 forks source link

404 when using `forward` in Plug.Router #81

Open wess opened 6 years ago

wess commented 6 years ago

My app is an umbrella with an api app that forwards to a versioned endpoint, like:

forward("/v1", to: V1)

inside of the V1 router, I have a forward to my auth handler:

forward("/auth", to: V1.Routes.Auth)

and my auth handler looks like:

defmodule V1.Routes.Auth do
  use Plug.Router
  require Logger

  plug(:match)
  plug(:fetch_query_params)
  plug(Ueberauth)
  plug(:dispatch)

  get "/:provider/callback" do
    conn
    |> send_resp(200, %{success: "facebook"})
  end
  match _ do
    conn
    |> send_resp(404, %{error: "Not found"})
  end
end

I have facebook configured and when ever I go to the url: /v1/auth/facebook I get a 404. Now if I move that /auth forward in to main api router, then /auth/facebook will work, but if I add V1 in front of that url, within the forward at all, I get a 404. I have tried setting the base path in the config, and explicitly setting the paths for the facebook strategy config.

Im not using Phoenix for this, it's pure plug.

doomspork commented 6 years ago

Howdy @wess, given that your second router is V1.Routes.Auth should your first forward to be forward("/v1", to: V1.Routes) instead of just V1?

Do you have a sample project I could peek at?

wess commented 6 years ago

here's all the codes:

API Router:

defmodule Api do
  require Logger
  use Responses
  use Plug.Router

  plug(Plug.Logger)

  plug(
    Plug.Static,
    at:   "/public",
    from: "public"
  )

  plug(:match)
  plug(:dispatch)

  forward("/v1", to: V1)

  get "/" do
    conn
    |> send_file(:ok, "public/index.html")
  end

  match _ do
    conn
    |> json(404, %{error: "not found"})
  end

end

V1 ROUTER

defmodule V1 do
  use Router

  plug(Plug.Logger)

  plug(:match)
  plug(:dispatch)

  forward("/user", to: V1.Routes.User)
  forward("/auth", to: V1.Routes.Auth)

  match _ do
    conn
    |> json(404, %{error: "Not found"})
  end

end

AUTH ROUTER:

defmodule V1.Routes.Auth do
  use Plug.Router
  require Logger

  plug(:match)
  plug(:fetch_query_params)
  plug(Ueberauth)
  plug(:dispatch)

  get "/:provider/callback" do
    conn
    |> send_resp(200, %{success: "facebook"})
  end
  match _ do
    conn
    |> send_resp(404, %{error: "Not found"})
  end
end