import { NextApiHandler } from 'next' ; import getConfig from 'next/config' ; import auth0 from '../../../src/utility/auth0' ; import axios , { AxiosInstance , Method as AxiosMethod } from 'axios' ; import { AccessTokenError } from '@auth0/nextjs-auth0' ; const { serverRuntimeConfig : config } = getConfig ( ) ; export const identityAxios = axios . create ( { baseURL : config . remoteApi . host , headers : { 'x-api-key' : config . remoteApi . apiKey , } , } ) ; const handleIdentityApiRequest = auth0 . withApiAuthRequired ( async ( req , res ) => { try { const { accessToken } = await auth0 . getAccessToken ( req , res ) ; const path = `/users/` + ( req . query [ 'users' ] as string [ ] ) . join ( '/' ) ; const remoteResponse = await identityAxios . request ( { url : path , method : req . method as AxiosMethod , data : req . body , headers : { ... identityAxios . defaults . headers . common , Authorization : `Bearer ${ accessToken }` , } , validateStatus : ( status ) => status >= 200 && status < 500 , } ) . catch ( error => error . response ) ; res . status ( remoteResponse . status ) . send ( remoteResponse . data ) ; } catch ( e ) { if ( e instanceof AccessTokenError ) { console . error ( 'Unexpected authentication error' , e ) ; res . status ( 401 ) . send ( e ) ; } else { throw e ; } } } ) ; export default handleIdentityApiRequest ;