mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
fix(device-pairing): clean up push subscriptions on admin reset and share URL helpers
The admin password reset finds the admin case-insensitively and deletes the push subscriptions of the sessions it ends. The app re-sends its push subscription once per signed-in session so revoke reaches re-signed-in devices, and the upsert keeps the stored device name. Plain-HTTP and URL parsing checks are shared helpers.
This commit is contained in:
1 parent
cbe4f7ed9e
commit
6e398c4d40
21 files changed
+540
-85
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
urlBase64ToUint8Array,
|
||||
} from "@/lib/serviceWorker";
|
||||
import type { NotificationPreferences } from "@/api/types/settings";
|
||||
import type { PushSubscriptionRecord } from "@opencode-manager/shared/types";
|
||||
import { DEFAULT_NOTIFICATION_PREFERENCES } from "@opencode-manager/shared/schemas";
|
||||
|
||||
type PermissionState = NotificationPermission | "unsupported";
|
||||
@@ -16,6 +17,18 @@ function getPermissionState(): PermissionState {
|
||||
return Notification.permission;
|
||||
}
|
||||
|
||||
export async function sendRegisteredSubscription(
|
||||
deviceName?: string
|
||||
): Promise<{ subscription: PushSubscriptionRecord } | null> {
|
||||
const registration = await getServiceWorkerRegistration();
|
||||
if (!registration) return null;
|
||||
|
||||
const existingSubscription = await registration.pushManager.getSubscription();
|
||||
if (!existingSubscription) return null;
|
||||
|
||||
return notificationsApi.subscribe(existingSubscription.toJSON(), deviceName);
|
||||
}
|
||||
|
||||
export function useNotifications() {
|
||||
const queryClient = useQueryClient();
|
||||
const { preferences, updateSettings } = useSettings();
|
||||
@@ -44,16 +57,13 @@ export function useNotifications() {
|
||||
|
||||
const subscribeMutation = useMutation({
|
||||
mutationFn: async (deviceName?: string) => {
|
||||
const registered = await sendRegisteredSubscription(deviceName);
|
||||
if (registered) return registered;
|
||||
|
||||
const reg = await getServiceWorkerRegistration();
|
||||
if (!reg) throw new Error("Service worker not registered");
|
||||
if (!vapidData?.publicKey) throw new Error("VAPID key not available");
|
||||
|
||||
const existingSub = await reg.pushManager.getSubscription();
|
||||
if (existingSub) {
|
||||
const json = existingSub.toJSON();
|
||||
return notificationsApi.subscribe(json, deviceName);
|
||||
}
|
||||
|
||||
const subscription = await reg.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlBase64ToUint8Array(vapidData.publicKey).buffer as ArrayBuffer,
|
||||
|
||||
Reference in new issue
Block a user