-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrevalidate-modal.js
78 lines (66 loc) · 2.33 KB
/
revalidate-modal.js
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
/**
* WordPress dependencies
*/
import { useCallback, useContext, useEffect, useRef } from '@wordpress/element';
import { GlobalContext } from '../script';
import { Modal } from '@wordpress/components';
import { useMergeRefs, useFocusableIframe } from '@wordpress/compose';
import { refreshRecord } from '../utilities';
export default function RevalidateModal() {
const { navigateToScreen } = useContext( GlobalContext );
const goBack = useCallback( ( event ) => {
event.preventDefault();
navigateToScreen( 'account-status' );
}, [] );
return (
<Modal
title="Two-Factor Authentication"
onRequestClose={ goBack }
className="wporg-2fa__revalidate-modal"
// Temporary workaround until https://github.com/WordPress/gutenberg/issues/40912 is fixed.
// Without this the modal immediately closes in Firefox, see https://github.com/WordPress/wporg-two-factor/issues/180
shouldCloseOnClickOutside={ false }
>
<p>To update your two-factor options, you must first revalidate your session.</p>
<RevalidateIframe />
</Modal>
);
}
function RevalidateIframe() {
const {
setGlobalNotice,
user: { userRecord },
} = useContext( GlobalContext );
const { record } = userRecord;
const ref = useRef();
useEffect( () => {
async function maybeRefreshUser( { data: { type, message } = {} } ) {
if ( type !== 'reValidationComplete' ) {
return;
}
setGlobalNotice( message || 'Two-Factor confirmed' );
// Pretend that the expires_at is in the future (+1hr), this provides a 'faster' UI.
// This intentionally doesn't use `edit()` to prevent it attempting to update it on the server.
record[ '2fa_revalidation' ].expires_at = new Date().getTime() / 1000 + 3600;
// Refresh the user record, to fetch the correct 2fa_revalidation data.
try {
await refreshRecord( userRecord );
} catch ( error ) {
// TODO: handle error more properly here, likely by showing a error notice
// eslint-disable-next-line no-console
console.error( 'Failed to refresh user record:', error );
}
}
window.addEventListener( 'message', maybeRefreshUser );
return () => {
window.removeEventListener( 'message', maybeRefreshUser );
};
}, [] );
return (
<iframe
title="Two-Factor Revalidation"
ref={ useMergeRefs( [ ref, useFocusableIframe() ] ) }
src={ record[ '2fa_revalidation' ].revalidate_url }
/>
);
}