import url from 'url' ; type RedirectControllerParams = { prefix ? : string ; appOrigin ? : string ; } ; export default class RedirectController { constructor ( { prefix , appOrigin } = { } ) { window . addEventListener ( 'load' , ( ) => { const callbackUri = window . location . href ; try { if ( window . opener && window . opener . oAuthCallback ) { window . opener . oAuthCallback ( callbackUri ) ; window . close ( ) ; return ; } } catch ( e ) { } try { if ( appOrigin && window . opener && window . opener . postMessage ) { window . opener . postMessage ( { callbackUri } , appOrigin ) ; window . close ( ) ; return ; } } catch ( error ) { } const { query : { state } , } = url . parse ( callbackUri , true ) ; const uuid = state . split ( '-' ) . slice ( 1 ) . join ( '-' ) ; const key = `${ prefix }-${ uuid }-callbackUri` ; localStorage . removeItem ( key ) ; window . addEventListener ( 'storage' , ( e ) => { if ( e . key === key && ( ! e . newValue || e . newValue === '' ) ) { window . close ( ) ; } } ) ; localStorage . setItem ( key , callbackUri ) ; } ) ; } }