dwyl / elixir-auth-github

:octocat: Minimalist GitHub OAuth Authentication for Elixir Apps. Tested, Documented & Maintained. Setup in 5 mins. 🚀
GNU General Public License v2.0
38 stars 4 forks source link

Post to Github defaults to port 4000 #62

Closed ndrean closed 2 years ago

ndrean commented 2 years ago

If I change the default port and run PORT=4001 iex ..., then the click button navigates to localhost:4000/auth/github.callback...

nelsonic commented 2 years ago

Hi @ndrean thanks very much for opening this issue to share your feedback. 🙌

TCP Port 4000 is not hard-coded anywhere in the code: https://github.com/dwyl/elixir-auth-github/search?q=4000&type=code

My hunch is that you have defined 4000 in the Homepage URL when configuring your GItHub App: Application Details

I don't know if there is a way to dynamically change the port... you may need to update the GitHub App first. 💭 Please keep us informed with your progress. 🙏 We are very keen to iron out any kinks. 👍

nelsonic commented 2 years ago

@SimonLab given that you are the person on the team with the most recent experience of using this package, ❤️ if you can help investigate after @ndrean has had a second look, please do. 🤞

ndrean commented 2 years ago

Ahh yes! Thank you @nelsonic for this answer. So the code is perfect :) The point is I need to test a distributed app.

nelsonic commented 2 years ago

@ndrean cool. Very happy to be your [Remote] Rubber Duck Debugger. 😉

rubber-duc-debugging

Thanks for testing + using + starring the package. 🤩 If you have any issues with deployment, please let us know. 👍

ndrean commented 2 years ago

Thanks for sharing your code. Now since the redirectionlocalhost:4000 is hard-coded, I don't see how I run two nodes and reach them from my browser to test distributed mode.

nelsonic commented 2 years ago

Hmm… you may need to setup a second GitHub App with the 4001 Port to test. 💭

ndrean commented 2 years ago

Yes, doing this, but unhappy doing this. Couldn't find any info on setting a dynamic url in Github. Also Phoenix didn't implement a dotenv library just because you have to set the env variables in production. They totally missed the point of dotenv because you have to paste the creds. Now with two versions, I will have to do it anyway \o/

nelsonic commented 2 years ago

The other option could be to run another server (e.g: nginx) on your localhost. 🏎️ Then proxy both Phoenix Apps running on ports 4000 and 4001. 2️⃣ We've done this before: /nginx-proxy-multiple-apps.md + nginx-phoenix-basic.conf You could do the whole setup with a single Dockerfile if you're comfortable with that. 🐳 We're very happy to help you figure this out if you need us. 💭

Edit: this will work well in production too. 🚀 P.S. if you need something specific in terms of environment variables, please open an issue on https://github.com/dwyl/envar we agree, there's a gap in Phoenix-land. 💭

ndrean commented 2 years ago

Found and tested something easy: dotenv-parser. It does the trick! Just create a ".env" with the two keys, gitignore it and paste the lines below in "runtimes.exs". No more copy/pasting :)

# /config/runtime.exs
if Config.config_env() == :dev,
  do: DotenParser.load_file("./config/.env")

config :elixir_auth_github,
  client_id: System.fetch_env!("GITHUB_CLIENT_ID"),
  client_secret: System.fetch_env!("GITHUB_CLIENT_SECRET")

Oops, missed your envar library. I checked it and quick feedback. From the same ".env" file, instead of calling DotenvParser.load_file and System.fetch_env!, I used Envar.load and Envar.get. I am able to run two sessions backed with two connected nodes, one on "localhost:4000" and another one on "localhost:4001" (picked two different Github credentials). I have an error with Envar whilst it is ok with DotenvParser. In case of any interest, I can explore the problem later. As a noob with Elixir/Phoenix, this build-time/runtime config is clear as mud in the sense that you don't really know what are runtimes vars vs build-time. SECRET_KEY_BASE is probably needed at compile-time, whilst the Github creds at runtime? I just can't use ".env" to set this :secret_key_base for "MyApp.Endpoint", need to hardcode it.

I know it's the wrong feed, I just post it here and am happy to erase and open all this in the correct Envvar feed, but in order not to forget, I post it here. When opening the second tab, I get:

[error] #PID<0.590.0> running PwdlessGsWeb.Endpoint (connection #PID<0.588.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /auth/github/callback?code=23a329a55803257d30d8
** (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: {:error, %{"error" => "bad_verification_code", "error_description" => "The code passed is incorrect or expired.", "error_uri" => "https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#bad-verification-code"}}
        (pwdless_gs 1.0.0) lib/pwdless_gs_web/controllers/github_auth_controller.ex:12: PwdlessGsWeb.GithubAuthController.index/2
ndrean commented 2 years ago

Very first try with Caddy to reverse proxy two load-balanced nodes reached at "localhost:5000" . I will dockerize a release (adopting Elixir!) and test your solution.

:5000 {
  reverse_proxy 127.0.0.1:4000 127.0.0.1:4001 {
    lb_policy round_robin

    header_up Host {host}
    header_up Host {upstream_hostport}
    header_up X-Real-IP {remote_host}
  }
  file_server
  encode zstd gzip

  log {
    output stdout
    format console
  }
}
nelsonic commented 2 years ago

https://github.com/caddyserver/caddy 👀