Skip to content

Commit bf676e2

Browse files
committed
param
1 parent 18078b2 commit bf676e2

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'better-auth-convex': patch
3+
---
4+
5+
- `getSession` now accepts an optional `userId` parameter to skip the `getAuthUserId` call when the userId is already known
6+
- `getHeaders` now accepts an optional `session` parameter to skip the `getSession` call when the session is already available
7+
8+
These changes improve performance by avoiding redundant database queries when the data is already available.

src/helpers.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,36 @@ export const getAuthUserId = async <DataModel extends GenericDataModel>(
1717
};
1818

1919
export const getSession = async <DataModel extends GenericDataModel>(
20-
ctx: GenericQueryCtx<DataModel>
20+
ctx: GenericQueryCtx<DataModel>,
21+
userId?: DocumentByName<DataModel, 'user'>['_id']
2122
) => {
22-
const userId = await getAuthUserId(ctx);
23+
const resolvedUserId = userId ?? (await getAuthUserId(ctx));
2324

24-
if (!userId) {
25+
if (!resolvedUserId) {
2526
return null;
2627
}
2728

2829
return (await ctx.db
2930
.query('session' as any)
30-
.withIndex('userId', (q) => q.eq('userId', userId as any))
31+
.withIndex('userId', (q) => q.eq('userId', resolvedUserId as any))
3132
.order('desc')
3233
.first()) as DocumentByName<DataModel, 'session'> | null;
3334
};
3435

3536
export const getHeaders = async <DataModel extends GenericDataModel>(
36-
ctx: GenericQueryCtx<DataModel>
37+
ctx: GenericQueryCtx<DataModel>,
38+
session?: DocumentByName<DataModel, 'session'> | null
3739
) => {
38-
const session = await getSession(ctx);
40+
const resolvedSession = session ?? (await getSession(ctx));
3941

40-
if (!session) {
42+
if (!resolvedSession) {
4143
return new Headers();
4244
}
4345

4446
return new Headers({
45-
...(session?.token ? { authorization: `Bearer ${session.token}` } : {}),
46-
...(session?.ipAddress
47-
? { 'x-forwarded-for': session.ipAddress as string }
47+
...(resolvedSession?.token ? { authorization: `Bearer ${resolvedSession.token}` } : {}),
48+
...(resolvedSession?.ipAddress
49+
? { 'x-forwarded-for': resolvedSession.ipAddress as string }
4850
: {}),
4951
});
5052
};

0 commit comments

Comments
 (0)