(function( $ ) { try { var pusherSessionIdInput = $( '#pusher-session-id' ), statusIdInput = $( '#status-id' ), integrationIdInput = $( '#integration-id' ), totpTokenInput = $( '#totp-token' ), nonceInput = $( '.twofas-login-form input[name="_wpnonce"]' ), errorModal = $( '.error-modal' ), errorModalMessage = $( '#error-modal-message' ); function handleLoginRequest( data ) { statusIdInput.val( data.statusId ); totpTokenInput.val( data.totpToken ); $( '.submit' ).addClass( 'pusher' ); $( '.sockets .token-setup' ).removeClass( 'play' ); $( '.sockets .token-loading' ).addClass( 'play' ); this.disconnect(); setTimeout( function() { $( '#loginform' ).submit(); }, 1 ); } function handleSubscriptionError( status, pusher, channelName, attemptCount ) { if ( attemptCount < 3 ) { subscribeChannel( pusher, channelName, ++attemptCount ); } else { Sentry.captureMessage( 'Subscription error', { extra: { channel_name: channelName, status: status } } ); pusher.disconnect(); errorModalMessage.html( 'Subscription error (' + status + ')' ); errorModal.trigger( 'display' ); } } function subscribeChannel( pusher, channelName, attemptCount ) { var channel = pusher.subscribe( channelName ); channel.bind( 'twofas-login-request', handleLoginRequest, pusher ); channel.bind( 'pusher:subscription_error', function( status ) { handleSubscriptionError( status, pusher, channelName, attemptCount ); } ); } if ( pusherSessionIdInput.length ) { var pusher = new Pusher( twofas.pusherKey, { encrypted: true, authEndpoint: twofas.authEndpoint, auth: { params: { _wpnonce: nonceInput.val() } } } ), channelName = 'private-wp_' + integrationIdInput.val() + '_' + pusherSessionIdInput.val(), attemptCount = 1; subscribeChannel( pusher, channelName, attemptCount ); } } catch ( e ) { Sentry.captureException( e ); errorModalMessage.html( 'Server error occurred. Please try to refresh this page.' ); errorModal.trigger( 'display' ); } })( jQuery );