import { ModuleConfig , ModuleSessionInfo } from '@graphql-modules/core' ; import { getClientIp } from 'request-ip' ; import { AccountsRequest , AccountsModuleConfig } from '../modules' ; export const context = ( moduleName ) => async ( { req , connection } , _ , { injector } ) => { if ( connection ) { return connection . context ; } if ( ! req ) { return { ip : '' , userAgent : '' , infos : { ip : '' , userAgent : '' , } , } ; } const config = injector . get ( ModuleConfig ( moduleName ) ) ; const headerName = config . headerName || 'Authorization' ; let authToken = ( req . headers [ headerName ] || req . headers [ headerName . toLowerCase ( ) ] ) as string ; authToken = authToken && authToken . replace ( 'Bearer ' , '' ) ; let user ; if ( authToken && ! config . excludeAddUserInContext ) { try { user = await config . accountsServer . resumeSession ( authToken ) ; } catch ( error ) { } } const ip = getClientIp ( req ) ; let userAgent = ( req . headers [ 'user-agent' ] as string ) || '' ; if ( req . headers [ 'x-ucbrowser-ua' ] ) { userAgent = req . headers [ 'x-ucbrowser-ua' ] as string ; } return { authToken , user , userId : user && user . id , userAgent , ip , infos : { userAgent , ip , } , } ; } ;