Login Example with hogMind

Complete Code Example

<!DOCTYPE html>
<html>
<head>
    <title>Login with hogMind</title>
</head>
<body>
    <form id="loginForm">
        <input type="text" id="username" required>
        <input type="password" id="password" required>
        <button type="submit">Login</button>
    </form>
    
    <script>
        // hogMind Integration
        async function trackHogMindFailedAttempt(username) {
            const deviceInfo = {
                browser: getBrowserInfo(),
                device: getDeviceInfo(),
                device_version: getDeviceVersion(),
                country: await getCountryInfo(),
                region: await getRegionInfo(),
                wifi_name: 'Not available'
            };
            
            try {
                // Use local endpoint for same-domain integration
                const apiUrl = '/hogmind/api/track';
                const response = await fetch(apiUrl, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        ip: await getClientIP(),
                        device_info: deviceInfo
                    })
                });
                
                const data = await response.json();
                if (data.blocked) {
                    console.log('hogMind: IP blocked after 3 failed attempts');
                }
            } catch (error) {
                console.error('hogMind: Error', error);
            }
        }
        
        function getBrowserInfo() {
            const ua = navigator.userAgent;
            if (ua.includes('Chrome')) {
                const match = ua.match(/Chrome\/([0-9.]+)/);
                return 'Chrome ' + (match ? match[1] : '');
            }
            if (ua.includes('Firefox')) {
                const match = ua.match(/Firefox\/([0-9.]+)/);
                return 'Firefox ' + (match ? match[1] : '');
            }
            return 'Unknown';
        }
        
        function getDeviceInfo() {
            const ua = navigator.userAgent;
            if (ua.includes('Windows')) return 'Windows';
            if (ua.includes('Mac')) return 'macOS';
            if (ua.includes('Linux')) return 'Linux';
            if (ua.includes('Android')) return 'Android';
            if (ua.includes('iPhone')) return 'iOS';
            return 'Unknown';
        }
        
        async function getClientIP() {
            try {
                const response = await fetch('https://api.ipify.org?format=json');
                const data = await response.json();
                return data.ip;
            } catch (error) {
                return 'Unknown';
            }
        }
        
        async function getCountryInfo() {
            try {
                const ip = await getClientIP();
                const response = await fetch(`https://ipapi.co/${ip}/country_name/`);
                return await response.text();
            } catch (error) {
                return 'Unknown';
            }
        }
        
        async function getRegionInfo() {
            try {
                const ip = await getClientIP();
                const response = await fetch(`https://ipapi.co/${ip}/region/`);
                return await response.text();
            } catch (error) {
                return 'Unknown';
            }
        }
        
        // Login form handler
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            // Simulate login attempt (replace with your actual login logic)
            const loginSuccess = await attemptLogin(username, password);
            
            if (!loginSuccess) {
                // Track failed attempt with hogMind
                await trackHogMindFailedAttempt(username);
                showError('Invalid username or password');
            } else {
                showSuccess('Login successful!');
            }
        });
        
        async function attemptLogin(username, password) {
            // Replace with your actual login API call
            // This is just a demo
            return false; // Always fail for demo
        }
        
        function showError(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-error';
            alert.textContent = message;
            document.getElementById('alertContainer').innerHTML = '';
            document.getElementById('alertContainer').appendChild(alert);
        }
        
        function showSuccess(message) {
            const alert = document.createElement('div');
            alert.className = 'alert alert-success';
            alert.textContent = message;
            document.getElementById('alertContainer').innerHTML = '';
            document.getElementById('alertContainer').appendChild(alert);
        }
    </script>
</body>
</html>
Back to Main