import { NextApiResponse , NextApiRequest , NextApiHandler } from 'next' ; import { SessionCache } from '../session' ; import { assertReqRes } from '../utils/assert' ; category export type WithApiAuthRequired = ( apiRoute : NextApiHandler ) => NextApiHandler ; ignore export default function withApiAuthFactory ( sessionCache ) { return ( apiRoute ) => async ( req , res ) : Promise < void > => { assertReqRes ( req , res ) ; const session = sessionCache . get ( req , res ) ; if ( ! session || ! session . user ) { res . status ( 401 ) . json ( { error : 'not_authenticated' , description : 'The user does not have an active session or is not authenticated' } ) ; return ; } await apiRoute ( req , res ) ; } ; }