import { IncomingMessage , ServerResponse } from 'http' ; import { NextApiRequest , NextApiResponse } from 'next' ; import { TokenSet } from 'openid-client' ; import onHeaders from 'on-headers' ; import { Config , SessionCache as ISessionCache , CookieStore } from '../auth0-session' ; import Session , { fromJson , fromTokenSet } from './session' ; type NextApiOrPageRequest = IncomingMessage | NextApiRequest ; type NextApiOrPageResponse = ServerResponse | NextApiResponse ; export default class SessionCache implements ISessionCache { private cache : WeakMap < NextApiOrPageRequest , Session | null > ; constructor ( private config , private cookieStore ) { this . cache = new WeakMap ( ) ; } init ( req , res ) { if ( ! this . cache . has ( req ) ) { const [ json , iat ] = this . cookieStore . read ( req ) ; this . cache . set ( req , fromJson ( json ) ) ; onHeaders ( res , ( ) => this . cookieStore . save ( req , res , this . cache . get ( req ) , iat ) ) ; } } create ( req , res , session ) { this . cache . set ( req , session ) ; onHeaders ( res , ( ) => this . cookieStore . save ( req , res , this . cache . get ( req ) ) ) ; } delete ( req , res ) { this . init ( req , res ) ; this . cache . set ( req , null ) ; } isAuthenticated ( req , res ) { this . init ( req , res ) ; const session = this . cache . get ( req ) ; return ! ! session ?. user ; } getIdToken ( req , res ) { this . init ( req , res ) ; const session = this . cache . get ( req ) ; return session ?. idToken ; } set ( req , res , session ) { this . init ( req , res ) ; this . cache . set ( req , session ) ; } get ( req , res ) { this . init ( req , res ) ; return this . cache . get ( req ) ; } fromTokenSet ( tokenSet ) { return fromTokenSet ( tokenSet , this . config ) ; } }