|
|
import { getQueryArg } from '@wordpress/url'; |
|
|
import { translate } from 'i18n-calypso'; |
|
|
import React, { useEffect } from 'react'; |
|
|
import DocumentHead from 'calypso/components/data/document-head'; |
|
|
import twoStepAuthorization from 'calypso/lib/two-step-authorization'; |
|
|
import ReauthRequiredComponent from 'calypso/me/reauth-required'; |
|
|
import './style.scss'; |
|
|
|
|
|
export default function ReauthRequired() { |
|
|
useEffect( () => { |
|
|
const handleSuccess = () => { |
|
|
const redirectTo = getQueryArg( window.location.search, 'redirect_to' ); |
|
|
|
|
|
if ( typeof redirectTo === 'string' && redirectTo ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
const url = new URL( redirectTo, window.location.origin ); |
|
|
if ( url.origin === window.location.origin || redirectTo.startsWith( '/' ) ) { |
|
|
|
|
|
window.location.href = url.href; |
|
|
} else { |
|
|
|
|
|
console.warn( `Skipping potentially unsafe redirect to: ${ redirectTo }` ); |
|
|
|
|
|
|
|
|
} |
|
|
} catch ( e ) { |
|
|
|
|
|
|
|
|
console.error( `Invalid redirect URL: ${ redirectTo }`, e ); |
|
|
} |
|
|
} else { |
|
|
|
|
|
|
|
|
} |
|
|
}; |
|
|
|
|
|
const handleAuthStateChange = () => { |
|
|
if ( ! twoStepAuthorization.isReauthRequired() ) { |
|
|
handleSuccess(); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
twoStepAuthorization.on( 'change', handleAuthStateChange ); |
|
|
|
|
|
return () => { |
|
|
twoStepAuthorization.off( 'change', handleAuthStateChange ); |
|
|
}; |
|
|
}, [] ); |
|
|
|
|
|
return ( |
|
|
<> |
|
|
<DocumentHead title={ translate( 'Reauth Required' ) } /> |
|
|
<ReauthRequiredComponent twoStepAuthorization={ twoStepAuthorization } /> |
|
|
</> |
|
|
); |
|
|
} |
|
|
|