user-login.js
133 lines
| 1 | /** |
| 2 | * Authenticate login. |
| 3 | */ |
| 4 | async function authenticate() { |
| 5 | |
| 6 | let token, remember_device, provider = ''; |
| 7 | |
| 8 | const loginForm = document.getElementById('loginform'); |
| 9 | let wp_2fa_submit = document.getElementById('submit'); |
| 10 | |
| 11 | let user_id = document.getElementById('wp-auth-id'); |
| 12 | if (!user_id) { |
| 13 | showError(wp.i18n.__('User ID not found.')); |
| 14 | } else { |
| 15 | user_id = user_id.value; |
| 16 | } |
| 17 | |
| 18 | if ( document.getElementsByName('authcode') && document.getElementsByName('authcode').length > 0) { |
| 19 | token = document.getElementsByName('authcode')[0].value; |
| 20 | } else if (document.getElementById('authcode')) { |
| 21 | token = document.getElementById('authcode').value; |
| 22 | } else { |
| 23 | showError(wp.i18n.__('Authentication code not found.')); |
| 24 | throw new Error('Authentication code not found.'); |
| 25 | } |
| 26 | |
| 27 | if ( '' === token.trim() ) { |
| 28 | showError(wp.i18n.__('Authentication code can not be empty.')); |
| 29 | throw new Error('Authentication code can not be empty.'); |
| 30 | } |
| 31 | |
| 32 | if ( document.getElementsByName('provider') && document.getElementsByName('provider').length > 0) { |
| 33 | provider = document.getElementsByName('provider')[0].value; |
| 34 | } else if (document.getElementById('provider')) { |
| 35 | provider = document.getElementById('provider').value; |
| 36 | } else { |
| 37 | showError(wp.i18n.__('Provider is not provided.')); |
| 38 | throw new Error('Provider is not provided.'); |
| 39 | } |
| 40 | |
| 41 | if (document.getElementById('remember_device') && document.getElementById('remember_device').checked) { |
| 42 | remember_device = true; |
| 43 | } |
| 44 | |
| 45 | // GET the response to the endpoint that calls. |
| 46 | try { |
| 47 | |
| 48 | let path = '/wp-2fa-methods/v1/login/' + user_id + '/' + token + '/' + provider + ((remember_device) ? '/' + remember_device : ''); |
| 49 | |
| 50 | const response = await window.wp.apiFetch({ |
| 51 | path: path, |
| 52 | method: 'GET', |
| 53 | }); |
| 54 | |
| 55 | if (true !== response.status) { |
| 56 | showError(response.message); |
| 57 | if ('' !== response.redirect_to) { |
| 58 | window.location.href = response.redirect_to; |
| 59 | } else { |
| 60 | loginForm.classList.add('shake'); |
| 61 | wp_2fa_submit.removeAttribute("disabled"); |
| 62 | throw new Error('2FA authentication failed.'); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ('' !== response.redirect_to) { |
| 67 | window.location.href = response.redirect_to; |
| 68 | } else { |
| 69 | |
| 70 | let redirect_to = document.getElementsByName('redirect_to'); |
| 71 | |
| 72 | if (!redirect_to.length) { |
| 73 | wp_2fa_submit.removeAttribute("disabled"); |
| 74 | throw new Error('Redirect URL not found.'); |
| 75 | } else { |
| 76 | redirect_to = redirect_to[0].value; |
| 77 | window.location.href = redirect_to; |
| 78 | } |
| 79 | } |
| 80 | } catch (error) { |
| 81 | throw error; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Show error message. |
| 87 | * |
| 88 | * @param {string} message Error message. |
| 89 | */ |
| 90 | function showError(message) { |
| 91 | const loginForm = document.getElementById('loginform'); |
| 92 | loginForm.classList.remove('shake'); |
| 93 | |
| 94 | let wp_2fa_submit = document.getElementById('submit'); |
| 95 | wp_2fa_submit.removeAttribute("disabled"); |
| 96 | |
| 97 | |
| 98 | if ( document.getElementById('login_error') ) { |
| 99 | document.getElementById('login_error').innerHTML = message; |
| 100 | } else { |
| 101 | |
| 102 | // Create Error element if not exists. |
| 103 | const errorElement = document.createElement('div'); |
| 104 | errorElement.id = 'login_error'; |
| 105 | errorElement.className = 'notice notice-error'; |
| 106 | errorElement.innerHTML = message; |
| 107 | errorElement.style.cssText = 'font-weight: bold;'; |
| 108 | |
| 109 | // Add error element before login form. |
| 110 | loginForm.parentNode.insertBefore(errorElement, loginForm); |
| 111 | } |
| 112 | |
| 113 | loginForm.classList.add('shake'); |
| 114 | } |
| 115 | |
| 116 | function onClick() { |
| 117 | let wp_2fa_submit = document.getElementById('submit'); |
| 118 | wp_2fa_submit.addEventListener('click', function (event) { |
| 119 | |
| 120 | // Handle the form data |
| 121 | event.preventDefault(); |
| 122 | event.target.setAttribute("disabled", true); |
| 123 | authenticate(); |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | window.wp.domReady(async () => { |
| 128 | let wp_2fa_submit = document.getElementById('submit'); |
| 129 | if (wp_2fa_submit) { |
| 130 | onClick(); |
| 131 | } |
| 132 | }); |
| 133 |