import { AxiosRequestConfig } from 'axios' ; import { signup , login } from '../services' ; import { restApi } from './calls' ; async function requestInterceptor ( config ) { const ignore = /\/auth\/token|users/g . test ( config . url ) ; if ( ignore ) return config ; let jwt = JSON . parse ( localStorage . getItem ( 'jwt' ) ) ; if ( ! jwt ) { const genRandom = ( ) => Math . round ( Math . random ( ) * Math . pow ( 10 , 20 ) ) . toString ( ) ; const email = `${ genRandom ( ) }@random.com` ; const password = genRandom ( ) ; await signup ( email , password ) ; jwt = await login ( email , password ) ; } config . headers . Authorization = `Bearer ${ jwt }` ; return config ; } restApi . interceptors . request . use ( requestInterceptor ) ;