import { strict as assert } from 'assert' ; import { NextApiResponse , NextApiRequest } from 'next' ; import { AuthorizationParameters , HandleCallback as BaseHandleCallback } from '../auth0-session' ; import { Session } from '../session' ; import { assertReqRes } from '../utils/assert' ; import { NextConfig } from '../config' ; import { HandlerError } from '../utils/errors' ; throws category export type AfterCallback = ( req : NextApiRequest , res : NextApiResponse , session : Session , state : { [ key ] : any ; } ) => Promise < Session > | Session ; category export interface CallbackOptions { afterCallback ? : AfterCallback ; /**
     * This is useful to specify in addition to {@Link BaseConfig.baseURL} when your app runs on multiple domains,
     * it should match {@Link LoginOptions.authorizationParams.redirect_uri}.
     */ redirectUri ? : string ; /**
     * This is useful to specify instead of {@Link NextConfig.organization} when your app has multiple
     * organizations, it should match {@Link LoginOptions.authorizationParams}.
     */ organization ? : string ; /**
     * This is useful for sending custom query parameters in the body of the code exchange request for use in rules.
     */ authorizationParams ? : Partial < AuthorizationParameters > ; } throws category export type HandleCallback = ( req : NextApiRequest , res : NextApiResponse , options ? : CallbackOptions ) => Promise < void > ; ignore const idTokenValidator = ( afterCallback ? , organization ? ) : AfterCallback => ( req , res , session , state ) => { if ( organization ) { assert ( session . user . org_id , 'Organization Id (org_id) claim must be a string present in the ID token' ) ; assert . equal ( session . user . org_id , organization , `Organization Id (org_id) claim value mismatch in the ID token; ` + `expected "${ organization }", found "${ session . user . org_id }"` ) ; } if ( afterCallback ) { return afterCallback ( req , res , session , state ) ; } return session ; } ; ignore export default function handleCallbackFactory ( handler , config ) { return async ( req , res , options = { } ) : Promise < void > => { try { assertReqRes ( req , res ) ; return await handler ( req , res , { ... options , afterCallback : idTokenValidator ( options . afterCallback , options . organization || config . organization ) } ) ; } catch ( e ) { throw new HandlerError ( e ) ; } } ; }