File size: 2,425 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import config from '@automattic/calypso-config';
import { useState } from 'react';
import { addQueryArgs } from 'calypso/lib/url';
interface UseLoginWindowProps {
onLoginSuccess: () => void;
onWindowClose: () => void;
}
interface UseLoginWindowReturn {
login: () => void;
createAccount: () => void;
close: () => void;
}
export default function useLoginWindow( {
onLoginSuccess,
onWindowClose,
}: UseLoginWindowProps ): UseLoginWindowReturn {
const [ loginWindow, setLoginWindow ] = useState< Window | null >( null );
const isBrowser: boolean = typeof window !== 'undefined';
if ( ! isBrowser ) {
return {
login: () => {},
createAccount: () => {},
close: () => {},
};
}
const environment = config( 'env_id' );
const args = {
action: 'verify',
service: 'wordpress',
// When in development, we need to pass an origin to allow the postMessage to know where to send the message.
origin: environment === 'development' ? new URL( window.location.href )?.hostname : undefined,
};
const redirectTo = addQueryArgs( args, 'https://wordpress.com/public.api/connect/' );
const loginURL = addQueryArgs( { redirect_to: redirectTo }, 'https://wordpress.com/log-in' );
const createAccountURL = addQueryArgs(
{
redirect_to: redirectTo,
ref: 'reader-lp',
},
'https://wordpress.com/start/account'
);
const windowFeatures =
'status=0,toolbar=0,location=1,menubar=0,directories=0,resizable=1,scrollbars=0,height=980,width=500';
const windowName = 'CalypsoLogin';
const waitForLogin = ( event: MessageEvent ) => {
if ( 'https://wordpress.com' !== event?.origin ) {
return;
}
if ( event?.data?.service === 'wordpress' ) {
onLoginSuccess();
}
};
const openWindow = ( url: string ) => {
const popupWindow = window.open( url, windowName, windowFeatures );
// Listen for logged in confirmation from the login window.
window.addEventListener( 'message', waitForLogin );
// Clean up loginWindow
const loginWindowClosed = setInterval( () => {
if ( popupWindow?.closed ) {
onWindowClose();
removeEventListener( 'message', waitForLogin );
clearInterval( loginWindowClosed );
}
}, 100 );
setLoginWindow( popupWindow );
};
const login = () => {
openWindow( loginURL );
};
const createAccount = () => {
openWindow( createAccountURL );
};
const close = () => {
loginWindow?.close();
};
return { login, createAccount, close };
}
|