Update session databaseHook uses Partial<Session> but passes Session to updateWithHook

I'm trying to make use of the updateSession databaseHook but am having issues with the types. Its params are a Partial<Session> but the expected return type is a full Session so even the following usage causes an error:
databaseHooks: {
session: {
update: {
before: async (session) => {
return {
data: session,
};
},
},
},
},
databaseHooks: {
session: {
update: {
before: async (session) => {
return {
data: session,
};
},
},
},
},
Type 'Partial<{ id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }>' is not assignable to type '{ id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }'.
Types of property 'id' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2322)
Type 'Partial<{ id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }>' is not assignable to type '{ id: string; token: string; userId: string; expiresAt: Date; createdAt: Date; updatedAt: Date; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }'.
Types of property 'id' are incompatible.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2322)
The issue seems to be here: https://github.com/better-auth/better-auth/blob/main/packages/better-auth/src/db/internal-adapter.ts#L412
GitHub
better-auth/packages/better-auth/src/db/internal-adapter.ts at main...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
2 Replies
Olly
OllyOP3mo ago
or am I suppose to query the DB and provide the whole session myself? seems wrong given the update function itself is only using the partial correctly Oh I think I've just misunderstood how the before hooks are supposed to work? Actually it works as I'd expect on user so I do think something is wrong
Olly
OllyOP3mo ago
I think I found the issue I just need to test it: https://github.com/better-auth/better-auth/pull/3396 edit: I've noticed the tests for the databasehooks seem sporadic which is why this wasn't caught. should I just throw one into the adapter tests or add more complex database hook testing?
GitHub
fix: allow partial session data in database hooks by othompson2 · ...
Summary Fixed session database hooks to accept Partial&amp;lt;Session&amp;gt; instead of requiring complete Session objects Makes session hooks consistent with user hooks behavior Resolves issues ...

Did you find this page helpful?