middleware icon indicating copy to clipboard operation
middleware copied to clipboard

feat(oauth-providers): availbility to pass state into oauth middlewares

Open buiducnhat opened this issue 1 year ago • 3 comments

Which middleware is the feature for?

@hono/oauth-providers

What is the feature you are proposing?

Currently, I see that @hono/oauth-providers supports discord, facebook, github, google, linkedin, x OAuth. I have just test for github and google and it works probably.

But I face a problem, I want to configure the URL that clients can be redirected after authorized successfully (redirect from Hono context, not from the providers, they are callbacks).

Then, I see that the googleAuth.ts has a options that can pass the state:

export function googleAuth(options: {
  scope: string[]
  login_hint?: string
  prompt?: 'none' | 'consent' | 'select_account'
  access_type?: 'online' | 'offline'
  client_id?: string
  client_secret?: string
  state?: string
  redirect_uri?: string
}): MiddlewareHandler {
  return async (c, next) => {
    const newState = options.state || getRandomState()

Then I can pass the clientRedirectUrl to the state, and extract it later.

But when dealing with Github (or other providers like the code I see), they don't have parameter state?

export function githubAuth(options: {
  client_id?: string
  client_secret?: string
  scope?: GitHubScope[]
  oauthApp?: boolean
  redirect_uri?: string
}): MiddlewareHandler {
  return async (c, next) => {
    const newState = getRandomState()

The above block is the githubAuth.ts for example

Are there any reasons that we cannot pass the state to the middlewares? From my side, I think it's inconsistent when we do like that. And of course, I love to have a parameter "state", it will help for my case.

buiducnhat avatar Dec 30 '24 16:12 buiducnhat

@buiducnhat Thank you for the PR.

Hey @monoald, Can you handle this?

yusukebe avatar Jan 06 '25 08:01 yusukebe

@buiducnhat Thank you for the PR.

Hey @monoald, Can you handle this?

@yusukebe Has my PR progress yet?

buiducnhat avatar Feb 13 '25 13:02 buiducnhat

@buiducnhat

Can you add tests?

yusukebe avatar Feb 17 '25 06:02 yusukebe