H
Honoβ€’3mo ago
jubalm

basicAuth context?

Just tried out hono and wanted to add basic auth and wanted to pull data from the Env but the context is any instead of what's set on the Bindings. what am i doing wrong here?
import { Hono } from 'hono'
import { basicAuth } from 'hono/basic-auth'

export type Env = {
PASS: string
}

const app = new Hono<{ Bindings: Env }>()

app.post(
'/test',
basicAuth({
verifyUser: (username, password, c) => {
return (username === 'admin' && password === c.env /* <-- does not autocomplete, Context<any, any>? */)
}
}),
async (c) => {
return c.body('OK')
}
)
import { Hono } from 'hono'
import { basicAuth } from 'hono/basic-auth'

export type Env = {
PASS: string
}

const app = new Hono<{ Bindings: Env }>()

app.post(
'/test',
basicAuth({
verifyUser: (username, password, c) => {
return (username === 'admin' && password === c.env /* <-- does not autocomplete, Context<any, any>? */)
}
}),
async (c) => {
return c.body('OK')
}
)
2 Replies
blurSkye πŸ‡΅πŸ‡ΈπŸ‰
To address the issue where the context c is of type any instead of the expected type with Bindings, you need to ensure that the context type is correctly inferred. This can be done by explicitly typing the context parameter in the verifyUser function.
app.post(
'/test',
basicAuth({
verifyUser: (username, password, c: Hono.Context<{ Bindings: Env }>) => {
return (username === 'admin' && password === c.env.PASS /* <-- should autocomplete now */)
}
}),
async (c) => {
return c.body('OK')
}
)
app.post(
'/test',
basicAuth({
verifyUser: (username, password, c: Hono.Context<{ Bindings: Env }>) => {
return (username === 'admin' && password === c.env.PASS /* <-- should autocomplete now */)
}
}),
async (c) => {
return c.body('OK')
}
)
Explanation: - Explicitly type the context parameter c in the verifyUser function as Hono.Context<{ Bindings: Env }> to ensure that TypeScript correctly infers the type and provides autocompletion for c.env.PASS.
jubalm
jubalmOPβ€’3mo ago
thanks for this @blurSkye πŸ‡΅πŸ‡ΈπŸ‰. slight adjustment was to import Context from hono as there wasn't a type Hono.Context on my setup. maybe i was expecting some generics implementation of basicAuth or verifyUser of some sorts but this should do
Want results from more Discord servers?
Add your server