Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Users to Specify Types for JWT Middleware Payload #2492

Open
NicoPlyley opened this issue Apr 10, 2024 · 4 comments
Open

Allow Users to Specify Types for JWT Middleware Payload #2492

NicoPlyley opened this issue Apr 10, 2024 · 4 comments
Labels
enhancement New feature or request.

Comments

@NicoPlyley
Copy link
Contributor

NicoPlyley commented Apr 10, 2024

What is the feature you are proposing?

When using the JWT middleware, end users currently cannot specify the type for the payload. The context variable is set to jwtPayload: any, and there have been multiple discussions by people on Discord asking how to set the types. It cannot be overridden in ContextVariableMap, so the only way to set it is by casting.

I am proposing that we either incorporate generic type parameters, for example, jwt<MyPayloadType>({ secret: '123' }), which can be backwards compatible.

Or we remove the following:

interface ContextVariableMap {
  jwtPayload: any;
}

And force the user to set the context variables themselves.

@NicoPlyley NicoPlyley added the enhancement New feature or request. label Apr 10, 2024
@yusukebe
Copy link
Member

Hi @NicoPlyley, Thank you for raising the issue.

This is related to #2249, #2313.

I'm thinking about this a long time. We are going to resolve this issue soon.

@BraydenGirard
Copy link

Thank you @NicoPlyley and @yusukebe this will solve my issues as well!

@bingtsingw
Copy link

bingtsingw commented Apr 22, 2024

@yusukebe Any chance to make JwtPayload not global? I have an app which use different auth middleware for different routes, but as long as I import hono/jwt, every context have the ts type of JwtPayload, which may lead to runtime error.

@yusukebe
Copy link
Member

@bingtsingw

Yes. I want to make it not global in the future. The following way should be recommended:

// factory.ts
import { drizzle, DrizzleD1Database } from 'drizzle-orm/d1'
import { Hono } from 'hono'

export const createHonoWithDB = () => {
  const app = new Hono<{
    Bindings: {
      D1: D1Database
    }
    Variables: {
      db: DrizzleD1Database
    }
  }>()
  app.use(async (c, next) => {
    c.set('db', drizzle(c.env.D1))
    await next()
  })
  return app
}
// app.ts
import { createHonoWithDB } from './factory'

const app = createHonoWithDB()

app.get('/', (c) => {
  c.var.db.select() //...
})

export default app

But, though it's a types matter, it causes a little breaking change. So I'm cautious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

4 participants