feat: add authorization header to device token regeneration
- Add Bearer token from localStorage to regenerate-token API request - Update code formatting for consistency (double quotes, indentation) This ensures the device token regeneration endpoint receives proper authentication via the Authorization header.
This commit is contained in:
@@ -2,96 +2,101 @@
|
|||||||
// Procedural style with proper types (no OOP)
|
// Procedural style with proper types (no OOP)
|
||||||
|
|
||||||
interface RegenerateTokenResponse {
|
interface RegenerateTokenResponse {
|
||||||
message: string;
|
message: string;
|
||||||
auth_token: string;
|
auth_token: string;
|
||||||
device: {
|
device: {
|
||||||
id: string;
|
id: string;
|
||||||
device_name: string;
|
device_name: string;
|
||||||
device_type: string;
|
device_type: string;
|
||||||
auth_token: string;
|
auth_token: string;
|
||||||
sync_enabled: boolean;
|
sync_enabled: boolean;
|
||||||
auto_sync: boolean;
|
auto_sync: boolean;
|
||||||
sync_frequency_minutes: number;
|
sync_frequency_minutes: number;
|
||||||
};
|
};
|
||||||
sync_urls?: {
|
sync_urls?: {
|
||||||
sync_url?: string;
|
sync_url?: string;
|
||||||
markup?: string;
|
markup?: string;
|
||||||
bookmark?: string;
|
bookmark?: string;
|
||||||
init?: string;
|
init?: string;
|
||||||
progress?: string;
|
progress?: string;
|
||||||
metadata?: string;
|
metadata?: string;
|
||||||
bookmarks?: string;
|
bookmarks?: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy sync URL or auth token to clipboard
|
// Copy sync URL or auth token to clipboard
|
||||||
function copyToClipboard(text: string, label: string): void {
|
function copyToClipboard(text: string, label: string): void {
|
||||||
navigator.clipboard.writeText(text)
|
navigator.clipboard
|
||||||
.then(() => {
|
.writeText(text)
|
||||||
const toast = (window as any).showToast;
|
.then(() => {
|
||||||
if (toast) {
|
const toast = (window as any).showToast;
|
||||||
toast.success(`${label} copied to clipboard`);
|
if (toast) {
|
||||||
}
|
toast.success(`${label} copied to clipboard`);
|
||||||
})
|
}
|
||||||
.catch((err: unknown) => {
|
})
|
||||||
console.error('Failed to copy:', err);
|
.catch((err: unknown) => {
|
||||||
const toast = (window as any).showToast;
|
console.error("Failed to copy:", err);
|
||||||
if (toast) {
|
const toast = (window as any).showToast;
|
||||||
toast.error('Failed to copy to clipboard');
|
if (toast) {
|
||||||
}
|
toast.error("Failed to copy to clipboard");
|
||||||
});
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Regenerate device token with confirmation
|
// Regenerate device token with confirmation
|
||||||
function regenerateDeviceToken(deviceId: string, event: Event): void {
|
function regenerateDeviceToken(deviceId: string, event: Event): void {
|
||||||
const confirmation = '⚠️ This will revoke current token and generate a new one.\n\n' +
|
const confirmation =
|
||||||
'The old token will immediately stop working.\n\n' +
|
"⚠️ This will revoke current token and generate a new one.\n\n" +
|
||||||
'You will need to update your device configuration with new token.\n\n' +
|
"The old token will immediately stop working.\n\n" +
|
||||||
'Continue?';
|
"You will need to update your device configuration with new token.\n\n" +
|
||||||
|
"Continue?";
|
||||||
|
|
||||||
if (!confirm(confirmation)) {
|
if (!confirm(confirmation)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const btn = event.target as HTMLButtonElement;
|
const btn = event.target as HTMLButtonElement;
|
||||||
const originalText = btn.innerHTML;
|
const originalText = btn.innerHTML;
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
btn.innerHTML = '🔄 Regenerating...';
|
btn.innerHTML = "🔄 Regenerating...";
|
||||||
|
|
||||||
fetch(`/api/devices/${deviceId}/regenerate-token`, {
|
const token = localStorage.getItem("token");
|
||||||
method: 'PUT',
|
fetch(`/api/devices/${deviceId}/regenerate-token`, {
|
||||||
headers: {
|
method: "PUT",
|
||||||
'Content-Type': 'application/json',
|
headers: {
|
||||||
}
|
"Content-Type": "application/json",
|
||||||
})
|
Authorization: `Bearer ${token}`,
|
||||||
.then((response: Response) => {
|
},
|
||||||
if (!response.ok) {
|
})
|
||||||
throw new Error('Failed to regenerate token');
|
.then((response: Response) => {
|
||||||
}
|
if (!response.ok) {
|
||||||
return response.json() as Promise<RegenerateTokenResponse>;
|
throw new Error("Failed to regenerate token");
|
||||||
})
|
}
|
||||||
.then((_data: RegenerateTokenResponse) => {
|
return response.json() as Promise<RegenerateTokenResponse>;
|
||||||
const toast = (window as any).showToast;
|
})
|
||||||
if (toast) {
|
.then((_data: RegenerateTokenResponse) => {
|
||||||
toast.success('Token regenerated successfully - update your device config');
|
const toast = (window as any).showToast;
|
||||||
}
|
if (toast) {
|
||||||
// Reload page to show new token
|
toast.success(
|
||||||
setTimeout(() => location.reload(), 1500);
|
"Token regenerated successfully - update your device config",
|
||||||
})
|
);
|
||||||
.catch((error: unknown) => {
|
}
|
||||||
console.error('Error:', error);
|
// Reload page to show new token
|
||||||
const toast = (window as any).showToast;
|
setTimeout(() => location.reload(), 1500);
|
||||||
if (toast) {
|
})
|
||||||
toast.error('Failed to regenerate token');
|
.catch((error: unknown) => {
|
||||||
}
|
console.error("Error:", error);
|
||||||
if (btn) {
|
const toast = (window as any).showToast;
|
||||||
btn.disabled = false;
|
if (toast) {
|
||||||
btn.innerHTML = originalText;
|
toast.error("Failed to regenerate token");
|
||||||
}
|
}
|
||||||
});
|
if (btn) {
|
||||||
|
btn.disabled = false;
|
||||||
|
btn.innerHTML = originalText;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export functions for global access (called from template onclick attributes)
|
// Export functions for global access (called from template onclick attributes)
|
||||||
window.copyToClipboard = copyToClipboard;
|
window.copyToClipboard = copyToClipboard;
|
||||||
window.regenerateDeviceToken = regenerateDeviceToken;
|
window.regenerateDeviceToken = regenerateDeviceToken;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user