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

404 when using forward in Plug.Router #81

Open
wess opened this issue Jul 26, 2018 · 2 comments
Open

404 when using forward in Plug.Router #81

wess opened this issue Jul 26, 2018 · 2 comments

Comments

@wess
Copy link

wess commented Jul 26, 2018

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
Copy link
Member

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
Copy link
Author

wess commented Jul 26, 2018

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

@yordis yordis added this to the v1.1 milestone Dec 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants