import { Action } from 'routing-controllers' ; import { Container } from 'typedi' ; import { Connection } from 'typeorm' ; import { Logger } from '../lib/logger' ; import { AuthService } from './AuthService' ; export function authorizationChecker ( connection ) { const log = new Logger ( __filename ) ; const authService = Container . get < AuthService > ( AuthService ) ; return async function innerAuthorizationChecker ( action , roles ) : Promise < boolean > { const userId = await authService . parseBasicAuthFromRequest ( action . request ) ; if ( userId === undefined ) { log . warn ( 'No credentials given' ) ; return false ; } console . log ( roles ) ; if ( roles [ 0 ] === 'customer' ) { action . request . user = await authService . validateCustomer ( userId ) ; if ( action . request . user === undefined ) { log . warn ( 'Invalid credentials given' ) ; return false ; } log . info ( 'Successfully checked credentials' ) ; return true ; } else { action . request . user = await authService . validateUser ( userId ) ; if ( action . request . user === undefined ) { log . warn ( 'Invalid credentials given' ) ; return false ; } log . info ( 'Successfully checked credentials' ) ; return true ; } } ; }