sergiodxa / remix-auth-oauth2

A OAuth2Strategy for Remix Auth
https://sergiodxa.github.io/remix-auth-oauth2/
MIT License
150 stars 56 forks source link

Allow setting redirectURI based on request object #93

Closed jimmycallin closed 3 months ago

jimmycallin commented 3 months ago

Hi!

Would it be possible to enable allowing to set the redirectURI based on the request object? In particular, I would like to be able to set the host of the redirectURI. In my current app environment, the hostname is not available as an environment variable and we automatically generate PR servers, making dynamically setting the redirectURI quite useful.

Thanks!

sergiodxa commented 3 months ago

Create the authenticator and strategy on the request, that way you can extract the hostname from the request.url.

export async function getAuthenticator(url: URL) {
  let authenticator = new Authenticator(sessionStorage, options)
  let options = { redirectURI: new URL("/auth/github/callback", url), ...moreOptions }
  authenticator.use(new OAuth2Strategy(options, verify)
  return authenticator;
}

And then in your loader:

export async function loader({ request }: LoaderFunctionArgs) {
  let url = new URL(request.url)
  let auth = getAuthenticator(url)
  await auth.authenticate(request, "oauth2", options)
  // rest of your code
}
jimmycallin commented 3 months ago

Aah, of course! I for some reason assumed authenticators needed to be defined on module level.

Thank you!