subhendukundu / worker-auth-providers

worker-auth-providers is an open-source providers to make authentication easy with workers. Very lightweight script which doesn't need a lot of dependencies. Plug it with any framework or template of workers.

Home Page:https://authc1.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can we make it so we can also pass the code in, instead of the request object?

tokenhousetopdev opened this issue · comments

I would like to an endpoint like:

/github/:userId

And use a post request so I can link user accounts via ouath. I understand I could do GET but it just doesn't feel right.

Are you trying to get user details from GitHub? Can you explain more of this?

I do something similar in my code:

export const validateCallbackBody = async (c: Context<Environment>): Promise<Request> => {
  const bodyParse = await c.req.json()
  const { code } = authValidation.oauthCallback.parse(bodyParse)
  const url = new URL(c.req.url)
  url.searchParams.set('code', code)
  const request = new Request(url.toString())
  return request
}

I have a post endpoint which I use to link oauth accounts. The frontend will post the code to /github/:userId to link the user to a github account.

The issue comes with that in the callback you have to pass in a request object which forces me to either create a new request object with the code as a query parameter (see above code), or only use GET requests.

export const githubCallback: Handler<Environment> = async (c) => {
  const config = getConfig(c.env)
  const request = await validateCallbackBody(c)
  const oauthRequest = github.users({
    options: {
      clientId: config.oauth.github.clientId,
      clientSecret: config.oauth.github.clientSecret
    },
    request
  })
  return oauthCallback(c, oauthRequest, authProviders.GITHUB)
}

Let me know if this helps, thanks!