Infer type is empty in hono

In the auth.ts file , while following the hono better-auth docs. It states to infer the user and session type use auth.$infer. But in my case that is an empty object. I am binding that to Hono app by const app = new Hono<{Binding: AuthType}> which according to docs should set the type for users and session. this is the type that i get back when using auth.api.getSession()
const session: {
session: unknown;
user: unknown;
} | null
const session: {
session: unknown;
user: unknown;
} | null
why is it unknown ?
Solution:
hello also check your config (like empty plugins array etc), had this kind of errors before too...
Jump to solution
8 Replies
aswnss
aswnssOP2w ago
export type AuthType = {
Variables: {
user: typeof auth.$Infer.Session.user | null;
session: typeof auth.$Infer.Session.session | null;
};
};
export type AuthType = {
Variables: {
user: typeof auth.$Infer.Session.user | null;
session: typeof auth.$Infer.Session.session | null;
};
};
No description
Blank
Blank2w ago
try this
type SessionType = ReturnType<typeof auth.api.getSession>;
type SessionType = ReturnType<typeof auth.api.getSession>;
then use SessionType['user'] also I think you need to pass in the db for it to work
aswnss
aswnssOP2w ago
I do not get it .. why does this also have the session and use type as unknown ?
type SessionType = Promise<{
session: unknown;
user: unknown;
} | null> & {
type SessionType = Promise<{
session: unknown;
user: unknown;
} | null> & {
No description
aswnss
aswnssOP2w ago
even if i use Awaited<Promise>. the session is unknown
type SessionType = {
session: unknown;
user: unknown;
} | null
type SessionType = {
session: unknown;
user: unknown;
} | null
No description
Solution
PavidR
PavidR2w ago
hello also check your config (like empty plugins array etc), had this kind of errors before too
aswnss
aswnssOP2w ago
That was the reason.
aswnss
aswnssOP2w ago
another question in hono , is how should we bind the authtypes ? in the hono website the example given is like this
const router = new Hono<{ Bindings: AuthType }>({
strict: false,
})
const router = new Hono<{ Bindings: AuthType }>({
strict: false,
})
but then i am getting error calling c.get("user")
Blank
Blank2w ago
you need to pass it as variables
const router = new Hono<{ Variables: AuthType }>({
strict: false,
})
const router = new Hono<{ Variables: AuthType }>({
strict: false,
})
then in a middleware set the auth info with c.set

Did you find this page helpful?