/** * License Check Module * Verifies license on app load and redirects if invalid */ (function () { 'use strict'; // Check license immediately when script loads checkLicense(); /** * Main license check function */ function checkLicense() { const licenseData = localStorage.getItem('fxmath_license'); // No license found if (!licenseData) { redirectToLogin(); return; } try { const license = JSON.parse(licenseData); // Validate license structure if (!license.key || !license.type || !license.status) { console.warn('Invalid license data structure'); redirectToLogin(); return; } // Check if license is active if (license.status !== 'active') { console.warn('License is not active'); redirectToLogin(); return; } // Check expiry for limited licenses if (license.type === 'limited') { if (!license.expiry_date) { console.warn('Limited license missing expiry date'); redirectToLogin(); return; } const expiryDate = new Date(license.expiry_date); const now = new Date(); if (now > expiryDate) { console.warn('License has expired'); localStorage.removeItem('fxmath_license'); alert('Your license has expired. Please renew your license to continue using FxMathQuant.'); redirectToLogin(); return; } // Calculate days remaining const daysRemaining = Math.ceil((expiryDate - now) / (1000 * 60 * 60 * 24)); // Warn if license is expiring soon (7 days or less) if (daysRemaining <= 7 && daysRemaining > 0) { console.warn(`License expiring in ${daysRemaining} days`); showExpiryWarning(daysRemaining); } } // License is valid console.log('License validated successfully'); displayLicenseInfo(license); } catch (error) { console.error('License check error:', error); redirectToLogin(); } } /** * Redirect to login page */ function redirectToLogin() { // Only redirect if not already on login page if (!window.location.pathname.includes('login.html')) { window.location.href = 'login.html'; } } /** * Show expiry warning */ function showExpiryWarning(daysRemaining) { // Create warning banner if it doesn't exist let banner = document.getElementById('license-expiry-warning'); if (!banner) { banner = document.createElement('div'); banner.id = 'license-expiry-warning'; banner.style.cssText = ` position: fixed; top: 0; left: 0; right: 0; background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white; padding: 12px 20px; text-align: center; font-weight: 600; z-index: 10000; box-shadow: 0 2px 10px rgba(0,0,0,0.2); `; banner.innerHTML = ` ⚠️ Your license will expire in ${daysRemaining} day${daysRemaining !== 1 ? 's' : ''}. Please renew to continue using FxMathQuant. `; document.body.insertBefore(banner, document.body.firstChild); // Adjust body padding to account for banner document.body.style.paddingTop = '50px'; } } /** * Display license info in console (for debugging) */ function displayLicenseInfo(license) { console.log('%c License Information ', 'background: #667eea; color: white; font-weight: bold; padding: 5px 10px;'); console.log('Type:', license.type); console.log('Status:', license.status); if (license.type === 'limited') { console.log('Expiry Date:', license.expiry_date); console.log('Days Remaining:', license.days_remaining); } else { console.log('Expiry:', 'Never (Lifetime)'); } } /** * Expose logout function globally */ window.logoutLicense = function () { if (confirm('Are you sure you want to logout? You will need to enter your license key again.')) { localStorage.removeItem('fxmath_license'); window.location.href = 'login.html'; } }; })();