import { forbidden , HttpResponse , ok } from '@/application/helpers' ; import { Middleware } from '@/application/middlewares' ; import { RequiredString } from '@/application/validation' ; type HttpRequest = { authorization : string ; } ; type Model = Error | { userId : string ; } ; type Authorize = ( input : { token : string ; } ) => Promise < string > ; export class AuthenticationMiddleware implements Middleware { constructor ( private readonly authorize ) { } async handle ( { authorization } ) { if ( ! this . validate ( { authorization } ) ) return forbidden ( ) ; try { const userId = await this . authorize ( { token : authorization } ) ; return ok ( { userId } ) ; } catch { return forbidden ( ) ; } } private validate ( { authorization } ) { const error = new RequiredString ( authorization , 'authorization' ) . validate ( ) ; return error === undefined ; } }