Account Settings
Subscription & Usage
Usage This Period
0 / 0 API calls
API Keys
const APP_NAME = 'OpenSaaS';
function renderNavbar() {
document.getElementById('navbar').innerHTML = ``;
}
function renderFooter() {
document.getElementById('footer').innerHTML = ``;
}
function togglePwd(id, btn) { const i = document.getElementById(id); i.type = i.type === 'password' ? 'text' : 'password'; btn.textContent = i.type === 'password' ? '👁️' : '🔒'; }
async function loadProfile() {
try {
const user = await checkAuth();
document.getElementById('displayName').value = user.display_name || '';
document.getElementById('email').value = user.email || '';
} catch {}
}
async function loadSubscription() {
try {
const r = await fetch('/api/v1/subscriptions/me', { headers: getAuthHeader() });
if (r.ok) {
const sub = await r.json();
const planNames = { free: 'Free', pro: 'Pro', enterprise: 'Enterprise' };
document.getElementById('currentPlan').textContent = planNames[sub.plan_id] || sub.plan_id || 'Free';
if (sub.auto_renew === 0 || sub.status !== 'active') {
document.getElementById('cancelSection').classList.add('hidden');
} else {
document.getElementById('cancelSection').classList.remove('hidden');
}
}
} catch {}
try {
const r = await fetch('/api/v1/usage/me', { headers: getAuthHeader() });
if (r.ok) {
const usage = await r.json();
const usageSection = document.getElementById('usageSection');
usageSection.classList.remove('hidden');
const percent = usage.limit > 0 ? Math.min((usage.used / usage.limit) * 100, 100) : 0;
document.getElementById('usageBar').style.width = percent + '%';
document.getElementById('usageText').textContent = `${usage.used} / ${usage.limit} API calls`;
}
} catch {}
}
async function cancelSubscription() {
if (!confirm('Are you sure you want to cancel your subscription? You will lose access to premium features at the end of the current billing period.')) return;
try {
const r = await fetch('/api/v1/subscriptions/cancel', { method: 'POST', headers: getAuthHeader() });
if (r.ok) { alert('Subscription cancelled successfully'); loadSubscription(); }
else { alert('Failed to cancel subscription'); }
} catch { alert('Network error'); }
}
document.getElementById('profileForm').addEventListener('submit', async (e) => {
e.preventDefault();
try {
const r = await fetch('/api/v1/user/me', { method: 'PUT', headers: { ...getAuthHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify({ display_name: document.getElementById('displayName').value }) });
const msg = document.getElementById('profileMsg');
if (r.ok) {
msg.textContent = 'Changes saved successfully';
msg.className = 'px-4 py-3 rounded-lg text-sm bg-green-50 text-green-600';
} else {
const d = await r.json();
msg.textContent = d.detail || 'Failed to save';
msg.className = 'px-4 py-3 rounded-lg text-sm bg-red-50 text-red-600';
}
msg.classList.remove('hidden');
} catch {}
});
document.getElementById('passwordForm').addEventListener('submit', async (e) => {
e.preventDefault();
const newPwd = document.getElementById('newPwd').value;
const confirmPwd = document.getElementById('confirmPwd').value;
if (newPwd !== confirmPwd) {
const m = document.getElementById('pwdMsg');
m.textContent = 'Passwords do not match';
m.className = 'px-4 py-3 rounded-lg text-sm bg-red-50 text-red-600';
m.classList.remove('hidden');
return;
}
if (newPwd.length < 6 || newPwd.length > 20) {
const m = document.getElementById('pwdMsg');
m.textContent = 'Password must be between 6 and 20 characters';
m.className = 'px-4 py-3 rounded-lg text-sm bg-red-50 text-red-600';
m.classList.remove('hidden');
return;
}
try {
const r = await fetch('/api/v1/user/me/change-password', { method: 'POST', headers: { ...getAuthHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify({ current_password: document.getElementById('currentPwd').value, new_password: newPwd }) });
const m = document.getElementById('pwdMsg');
if (r.ok) {
m.textContent = 'Password changed successfully';
m.className = 'px-4 py-3 rounded-lg text-sm bg-green-50 text-green-600';
document.getElementById('passwordForm').reset();
} else {
const d = await r.json();
m.textContent = d.detail || 'Failed to change password';
m.className = 'px-4 py-3 rounded-lg text-sm bg-red-50 text-red-600';
}
m.classList.remove('hidden');
} catch {
const m = document.getElementById('pwdMsg');
m.textContent = 'Network error';
m.className = 'px-4 py-3 rounded-lg text-sm bg-red-50 text-red-600';
m.classList.remove('hidden');
}
});
async function loadApiKeys() {
try {
const r = await fetch('/api/v1/api-keys', { headers: getAuthHeader() });
const keys = r.ok ? await r.json() : [];
document.getElementById('apiKeys').innerHTML = keys.length === 0 ? 'No API keys yet
' : keys.map(k => `${k.key_hash ? k.key_hash.substring(0, 12) + '...' : 'N/A'}${k.is_active ? 'Active' : 'Revoked'}
`).join('');
} catch {}
}
async function createApiKey() {
const name = prompt('Key name:');
if (!name) return;
try {
const r = await fetch('/api/v1/api-keys', { method: 'POST', headers: { ...getAuthHeader(), 'Content-Type': 'application/json' }, body: JSON.stringify({ name }) });
const d = await r.json();
if (d.raw_key) { alert('Please save this key (shown only once):\n' + d.raw_key); }
loadApiKeys();
} catch {}
}
(async () => {
renderNavbar();
renderFooter();
if (await checkAuth()) {
loadProfile();
loadSubscription();
loadApiKeys();
}
})();