feat: add RegenerateDeviceToken API endpoint

- Add handler to regenerate device auth tokens
- Add PUT /api/devices/:id/regenerate-token route
- Returns new token and sync URLs for device configuration
This commit is contained in:
2026-02-13 12:12:28 -05:00
parent 8321149957
commit 81fbcfac11
3 changed files with 189 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
// Device Management - Token copy and regeneration
// Procedural style with proper types (no OOP)
interface RegenerateTokenResponse {
message: string;
auth_token: string;
device: {
id: string;
device_name: string;
device_type: string;
auth_token: string;
sync_enabled: boolean;
auto_sync: boolean;
sync_frequency_minutes: number;
};
sync_urls?: {
sync_url?: string;
markup?: string;
bookmark?: string;
init?: string;
progress?: string;
metadata?: string;
bookmarks?: string;
};
}
// Copy sync URL or auth token to clipboard
function copyToClipboard(text: string, label: string): void {
navigator.clipboard.writeText(text)
.then(() => {
const toast = (window as any).showToast;
if (toast) {
toast.success(`${label} copied to clipboard`);
}
})
.catch((err: unknown) => {
console.error('Failed to copy:', err);
const toast = (window as any).showToast;
if (toast) {
toast.error('Failed to copy to clipboard');
}
});
}
// Regenerate device token with confirmation
function regenerateDeviceToken(deviceId: string, event: Event): void {
const confirmation = '⚠️ This will revoke current token and generate a new one.\n\n' +
'The old token will immediately stop working.\n\n' +
'You will need to update your device configuration with new token.\n\n' +
'Continue?';
if (!confirm(confirmation)) {
return;
}
const btn = event.target as HTMLButtonElement;
const originalText = btn.innerHTML;
btn.disabled = true;
btn.innerHTML = '🔄 Regenerating...';
fetch(`/api/devices/${deviceId}/regenerate-token`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
}
})
.then((response: Response) => {
if (!response.ok) {
throw new Error('Failed to regenerate token');
}
return response.json() as Promise<RegenerateTokenResponse>;
})
.then((_data: RegenerateTokenResponse) => {
const toast = (window as any).showToast;
if (toast) {
toast.success('Token regenerated successfully - update your device config');
}
// Reload page to show new token
setTimeout(() => location.reload(), 1500);
})
.catch((error: unknown) => {
console.error('Error:', error);
const toast = (window as any).showToast;
if (toast) {
toast.error('Failed to regenerate token');
}
if (btn) {
btn.disabled = false;
btn.innerHTML = originalText;
}
});
}
// Export functions for global access (called from template onclick attributes)
window.copyToClipboard = copyToClipboard;
window.regenerateDeviceToken = regenerateDeviceToken;