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:
2026-02-16 09:18:02 -05:00
parent a0e9a2b6e6
commit c9ebc5b11a
+21 -16
View File
@@ -26,7 +26,8 @@ interface RegenerateTokenResponse {
// 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
.writeText(text)
.then(() => { .then(() => {
const toast = (window as any).showToast; const toast = (window as any).showToast;
if (toast) { if (toast) {
@@ -34,20 +35,21 @@ function copyToClipboard(text: string, label: string): void {
} }
}) })
.catch((err: unknown) => { .catch((err: unknown) => {
console.error('Failed to copy:', err); console.error("Failed to copy:", err);
const toast = (window as any).showToast; const toast = (window as any).showToast;
if (toast) { if (toast) {
toast.error('Failed to copy to clipboard'); 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;
@@ -56,33 +58,37 @@ function regenerateDeviceToken(deviceId: string, event: Event): void {
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...";
const token = localStorage.getItem("token");
fetch(`/api/devices/${deviceId}/regenerate-token`, { fetch(`/api/devices/${deviceId}/regenerate-token`, {
method: 'PUT', method: "PUT",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
} Authorization: `Bearer ${token}`,
},
}) })
.then((response: Response) => { .then((response: Response) => {
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to regenerate token'); throw new Error("Failed to regenerate token");
} }
return response.json() as Promise<RegenerateTokenResponse>; return response.json() as Promise<RegenerateTokenResponse>;
}) })
.then((_data: RegenerateTokenResponse) => { .then((_data: RegenerateTokenResponse) => {
const toast = (window as any).showToast; const toast = (window as any).showToast;
if (toast) { if (toast) {
toast.success('Token regenerated successfully - update your device config'); toast.success(
"Token regenerated successfully - update your device config",
);
} }
// Reload page to show new token // Reload page to show new token
setTimeout(() => location.reload(), 1500); setTimeout(() => location.reload(), 1500);
}) })
.catch((error: unknown) => { .catch((error: unknown) => {
console.error('Error:', error); console.error("Error:", error);
const toast = (window as any).showToast; const toast = (window as any).showToast;
if (toast) { if (toast) {
toast.error('Failed to regenerate token'); toast.error("Failed to regenerate token");
} }
if (btn) { if (btn) {
btn.disabled = false; btn.disabled = false;
@@ -94,4 +100,3 @@ function regenerateDeviceToken(deviceId: string, event: Event): void {
// 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;