PageSourceSearch

https://www.birtingur.is/wp-content/themes/generik-child/askell/ajax-login-script.js?1763293838&ver=6.8.2

js birtingur.is collected 2026-10-02 22:22:05 UTC 11,270 bytes, 236 lines download raw bytes

1jQuery(document).ready(function($) {
2    // Check if empty on blur
3    jQuery('form#login_form #email').blur(function(){
4        if (jQuery(this).val() === '') {
5            jQuery(this).addClass('is-invalid');
6        } else {
7            jQuery(this).removeClass('is-invalid');
8        }
9    });
10    jQuery('form#login_form #password').blur(function(){
11        if (jQuery(this).val() === '') {
12            jQuery(this).addClass('is-invalid');
13        } else {
14            jQuery(this).removeClass('is-invalid');
15        }
16    });
17
18    // Track retry attempts
19    var retryCount = 0;
20    var maxRetries = 2;
21    
22    // Function to perform login
23    function performLogin(email, password, rememberme, security) {
24        return jQuery.ajax({
25            type: 'POST',
26            dataType: 'json',
27            url: ajax_login_object.ajaxurl,
28            timeout: 15000, // 15 second timeout
29            data: {
30                action: 'ajaxlogin',
31                email: email,
32                password: password,
33                rememberme: rememberme,
34                security: security
35            }
36        });
37    }
38    
39    // Perform AJAX login on form submit
40    jQuery('form#login_form').on('submit', function(e) {
41        e.preventDefault();
42        
43        // Get form data
44        var email = jQuery('form#login_form #email').val();
45        var password = jQuery('form#login_form #password').val();
46        var rememberme = jQuery('form#login_form #rememberme').is(':checked');
47        var security = jQuery('form#login_form input[name="security"]').val();
48        
49        // Clear previous messages
50        jQuery('form#login_form .login-status').html('');
51        
52        // Validate form
53        if (!email || !password) {
54            jQuery('form#login_form .login-status').html('<div class="alert alert-danger">Vinsamlegast fylltu út alla reiti</div>');
55            return;
56        }
57        
58        // Show loading message
59        jQuery('form#login_form .login-status').html('<div class="alert alert-info">' + (ajax_login_object.loadingmessage || 'Vinsamlegast bíðið...') + '</div>');
60        
61        // Reset retry count
62        retryCount = 0;
63        
64        // Function to handle login attempt
65        function attemptLogin() {
66            performLogin(email, password, rememberme, security)
67                .done(function(response) {
68                    console.log('AJAX response received:', response);
69                    console.log('Response type:', typeof response);
70                    if (response.data) {
71                        console.log('Response data:', response.data);
72                    }
73                    
74                    // Check if response is valid JSON
75                    if (typeof response === 'string') {
76                        try {
77                            response = JSON.parse(response);
78                        } catch(e) {
79                            console.error('Invalid JSON response:', response);
80                            jQuery('form#login_form .login-status').html('<div class="alert alert-danger">Ógild svör frá þjóni</div>');
81                            return false;
82                        }
83                    }
84                    
85                    // Handle WordPress JSON response format (success: false with data object)
86                    if (response.success === false && response.data) {
87                        // Check for nonce error first
88                        if (response.data.nonce_error === true) {
89                            // Nonce expired - refresh page to get new nonce
90                            console.log('Nonce error detected, refreshing page...');
91                            jQuery('form#login_form .login-status').html('<div class="alert alert-warning">Öryggislykill útrunninn. Endurnýja síðu...</div>');
92                            setTimeout(function() {
93                                window.location.reload();
94                            }, 1500);
95                            return false;
96                        }
97                        
98                        // Check if it's a login error (wrong credentials, etc.)
99                        if (response.data.loggedin === false) {
100                            // Show error message from data object
101                            var errorMsg = response.data.message || 'Innskráning mistókst';
102                            console.log('Login failed:', errorMsg);
103                            jQuery('form#login_form .login-status').html('<div class="alert alert-danger">' + errorMsg + '</div>');
104                            return false;
105                        }
106                        
107                        // If we get here, use data object for success case
108                        response = response.data;
109                    }
110                    
111                    // Handle success case
112                    if (response.loggedin === true) {
113                        console.log('Login successful, redirecting...');
114                        jQuery('form#login_form .login-status').html('<div class="alert alert-success">' + (response.message || 'Innskráning tókst!') + '</div>');
115                        setTimeout(function() {
116                            window.location.href = response.redirect || ajax_login_object.redirecturl;
117                        }, 500);
118                        return false;
119                    } else {
120                        // Show the actual error message from server
121                        console.log('Login failed:', response.message);
122                        jQuery('form#login_form .login-status').html('<div class="alert alert-danger">' + (response.message || 'Innskráning mistókst') + '</div>');
123                        return false;
124                    }
125                })
126                .fail(function(xhr, status, error) {
127                    console.error('AJAX error:', {
128                        status: status,
129                        error: error,
130                        statusCode: xhr.status,
131                        responseText: xhr.responseText,
132                        retryCount: retryCount
133                    });
134                    
135                    handleLoginError(xhr, status, error);
136                });
137        }
138        
139        // Function to handle errors with retry logic
140        function handleLoginError(xhr, status, error) {
141            var errorMessage = '';
142            
143            // Check specific error types
144            if (status === 'timeout') {
145                errorMessage = 'Tenging féll á tíma. Vinsamlegast reyndu aftur.';
146            } else if (xhr.status === 0) {
147                errorMessage = 'Nettenging virðist vera rofin. Athugaðu nettengingu þína.';
148            } else if (xhr.status === 404) {
149                errorMessage = 'Innskráningarþjónusta fannst ekki.';
150            } else if (xhr.status === 500) {
151                errorMessage = 'Villa á þjóni. Vinsamlegast reyndu aftur síðar.';
152            } else if (xhr.status === 403 || (xhr.responseText && (xhr.responseText.indexOf('-1') === 0 || xhr.responseText.trim() === '-1'))) {
153                // Nonce expired or 403 Forbidden - try to parse JSON error first
154                try {
155                    var jsonResponse = JSON.parse(xhr.responseText);
156                    if (jsonResponse.success === false && jsonResponse.data && jsonResponse.data.nonce_error) {
157                        errorMessage = jsonResponse.data.message || 'Öryggislykill útrunninn. Endurnýja síðu...';
158                        jQuery('form#login_form .login-status').html('<div class="alert alert-warning">' + errorMessage + '</div>');
159                        setTimeout(function() {
160                            window.location.reload();
161                        }, 1500);
162                        return;
163                    }
164                } catch(e) {
165                    // Not JSON, continue with normal handling
166                }
167                
168                // Nonce expired - refresh and retry
169                if (retryCount < 1) {
170                    retryCount++;
171                    console.log('Nonce expired, refreshing page...');
172                    errorMessage = 'Öryggislykill útrunninn. Síða endurhleðst...';
173                    jQuery('form#login_form .login-status').html('<div class="alert alert-warning">' + errorMessage + '</div>');
174                    setTimeout(function() {
175                        window.location.reload();
176                    }, 1500);
177                    return;
178                }
179            } else if (xhr.responseText && xhr.responseText.indexOf('0') === 0) {
180                errorMessage = 'Innskráning mistókst. Vinsamlegast athugaðu notandanafn og lykilorð.';
181            }
182            
183            // Retry logic for network errors
184            if (retryCount < maxRetries && (status === 'timeout' || xhr.status === 0)) {
185                retryCount++;
186                errorMessage += '<br>Reyni aftur... (' + retryCount + '/' + maxRetries + ')';
187                jQuery('form#login_form .login-status').html('<div class="alert alert-warning">' + errorMessage + '</div>');
188                
189                // Wait 2 seconds then retry
190                setTimeout(attemptLogin, 2000);
191                return;
192            }
193            
194            // Final error message with fallback option
195            if (!errorMessage) {
196                errorMessage = 'Villa kom upp við innskráningu.';
197            }
198            
199            // Add fallback link to standard WordPress login
200            errorMessage += '<br><br>';
201            errorMessage += '<strong>Vandamál við innskráningu?</strong><br>';
202            errorMessage += '<a href="/wp-login.php" class="btn btn-secondary btn-sm mt-2">Prófaðu hefðbundna innskráningu</a>';
203            
204            jQuery('form#login_form .login-status').html('<div class="alert alert-danger">' + errorMessage + '</div>');
205        }
206        
207        // Start login attempt
208        attemptLogin();
209    });
210    
211    // Add connection test on page load
212    jQuery(window).on('load', function() {
213        // Test if AJAX is working
214        jQuery.ajax({
215            url: ajax_login_object.ajaxurl,
216            type: 'POST',
217            data: { action: 'heartbeat' },
218            timeout: 5000,
219            success: function() {
220                console.log('AJAX connection test: OK');
221            },
222            error: function(xhr, status) {
223                console.warn('AJAX connection test failed:', status);
224                // Show warning if AJAX is not working
225                if (jQuery('form#login_form').length > 0) {
226                    jQuery('form#login_form').prepend(
227                        '<div class="alert alert-warning alert-dismissible fade show" role="alert">
227' +
228                        'Athugið: Vandamál gætu komið upp við innskráningu. <a href="/wp-login.php">Smelltu hér fyrir hefðbundna innskráningu</a>' +
229                        '<button type="button" class="close" data-dismiss="alert" aria-label="Close">' +
230                        '<span aria-hidden="true">&times;</span></button></div>'
231                    );
232                }
233            }
234        });
235    });
236});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.