sergiodxa / remix-auth

Simple Authentication for Remix
MIT License
1.94k stars 112 forks source link

GitHub Strategy with CF Workers #79

Closed lgastler closed 2 years ago

lgastler commented 2 years ago

Currently there is an error when you want to use the GitHub Strategy from remix-auth with Cloudflare Workers.

The error occurs when trying to retrieve profile information from the GitHub API. This request fails with an error status of 403. The error occurs because GitHub needs to force a user agent header, which is apparently not sent by the worker fetch. (https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required)

The solution is quite simple, just add a user agent header to the profile request.

However, I'm not sure how best to implement it. You could either set a fixed User-Agent (e.g. remix-auth-app) or forward the User-Agent from the incoming request. I think it makes no sense to have it configured as it probably only affects CloudFlare workers at the moment and other platforms automatically fill in the user agent.

https://github.com/sergiodxa/remix-auth/blob/42642e9c77a234a5227fda9b5ce17dc745416402/src/strategies/github.ts#L120-L126

let response = await fetch(this.userInfoURL, {
  headers: {
     Accept: "application/vnd.github.v3+json",
     Authorization: `token ${accessToken}`,
+    User-Agent: "remix-auth-app"
  },
});

Heres a example repo with a custom github.ts to fix the error.