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

Access user object returned by middleware #507

Open
ilshm opened this issue May 6, 2024 · 2 comments
Open

Access user object returned by middleware #507

ilshm opened this issue May 6, 2024 · 2 comments

Comments

@ilshm
Copy link

ilshm commented May 6, 2024

I am currently implementing authentication in my application using the Bearer Auth Middleware. I'm interested in accessing additional information about the user associated with the token. Does this functionality only work when using JWT (JSON Web Tokens)?

Example:

import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { bearerAuth } from 'https://deno.land/x/hono/middleware.ts'

const app = new Hono()
const token = 'honoiscool'

app.use(
  '/auth-verify-token/*',
  bearerAuth({
    verifyToken: async (token, c) => {
      // How to pass user from here to request?
      return token === dbQuery
    },
  })
)

app.get('/auth-verify-token/page', (c) => {
  return c.json({ message: `Your user id is ${c.user.id}` })
})

Thanks.

@yusukebe
Copy link
Member

yusukebe commented May 7, 2024

Hi @ilshm

Try to use c.set()/c.get(): https://hono.dev/api/context#set-get

@ilshm
Copy link
Author

ilshm commented May 7, 2024

@yusukebe thanks, but works strange and only in custom middleware :(

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

No branches or pull requests

2 participants