@@ -170,7 +170,7 @@ export class APIClass<
170170
171171 private _routes : { path : string ; options : Options ; endpoints : Record < string , string > } [ ] = [ ] ;
172172
173- public authMethods : ( ( ... args : any [ ] ) => any ) [ ] ;
173+ public authMethods : ( ( req : Request ) => Promise < IUser | undefined > ) [ ] ;
174174
175175 protected helperMethods : Map < string , ( ) => any > = new Map ( ) ;
176176
@@ -252,7 +252,7 @@ export class APIClass<
252252 return parseJsonQuery ( this ) ;
253253 }
254254
255- public addAuthMethod ( func : ( this : PartialThis , ... args : any [ ] ) => any ) : void {
255+ public addAuthMethod ( func : ( req : Request ) => Promise < IUser | undefined > ) : void {
256256 this . authMethods . push ( func ) ;
257257 }
258258
@@ -823,7 +823,7 @@ export class APIClass<
823823 ( operations [ method as keyof Operations < TPathPattern , TOptions > ] as Record < string , any > ) . action =
824824 async function _internalRouteActionHandler ( ) {
825825 if ( options . authRequired || options . authOrAnonRequired ) {
826- const user = await api . authenticatedRoute . call ( this , this . request ) ;
826+ const user = await api . authenticatedRoute . call ( api , this . request ) ;
827827 this . user = user ! ;
828828 this . userId = this . user ?. _id ;
829829 const authToken = this . request . headers . get ( 'x-auth-token' ) ;
@@ -973,11 +973,8 @@ export class APIClass<
973973 }
974974
975975 protected async authenticatedRoute ( req : Request ) : Promise < IUser | null > {
976- const headers = Object . fromEntries ( req . headers . entries ( ) ) ;
977-
978- const { 'x-user-id' : userId } = headers ;
979-
980- const userToken = String ( headers [ 'x-auth-token' ] ) ;
976+ const userId = req . headers . get ( 'x-user-id' ) ;
977+ const userToken = req . headers . get ( 'x-auth-token' ) ;
981978
982979 if ( userId && userToken ) {
983980 return Users . findOne (
@@ -990,6 +987,16 @@ export class APIClass<
990987 } ,
991988 ) ;
992989 }
990+
991+ for ( const method of this . authMethods ) {
992+ // eslint-disable-next-line no-await-in-loop -- we want serial execution
993+ const user = await method ( req ) ;
994+
995+ if ( user ) {
996+ return user ;
997+ }
998+ }
999+
9931000 return null ;
9941001 }
9951002
0 commit comments