@@ -17,34 +17,36 @@ export const getAuthUserId = async <DataModel extends GenericDataModel>(
1717} ;
1818
1919export 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
3536export 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