import { AuthenticateFn , AuthenticationBindings , AUTHENTICATION_STRATEGY_NOT_FOUND , USER_PROFILE_NOT_FOUND , } from '@loopback/authentication' ; import { Context , inject } from '@loopback/core' ; import { FindRoute , InvokeMethod , ParseParams , Reject , RequestContext , RestBindings , Send , SequenceHandler , } from '@loopback/rest' ; const SequenceActions = RestBindings . SequenceActions ; export class MySequence implements SequenceHandler { constructor ( @ inject ( RestBindings . Http . CONTEXT ) public ctx , @ inject ( SequenceActions . FIND_ROUTE ) protected findRoute , @ inject ( SequenceActions . PARSE_PARAMS ) protected parseParams , @ inject ( SequenceActions . INVOKE_METHOD ) protected invoke , @ inject ( SequenceActions . SEND ) public send , @ inject ( SequenceActions . REJECT ) public reject , @ inject ( AuthenticationBindings . AUTH_ACTION ) protected authenticateRequest ) { } async handle ( context ) { try { const { request , response } = context ; const route = this . findRoute ( request ) ; await this . authenticateRequest ( request ) ; const args = await this . parseParams ( request , route ) ; const result = await this . invoke ( route , args ) ; this . send ( response , result ) ; } catch ( error ) { if ( error . code === AUTHENTICATION_STRATEGY_NOT_FOUND || error . code === USER_PROFILE_NOT_FOUND ) { Object . assign ( error , { statusCode : 401 } ) ; } this . reject ( context , error ) ; } } }