diff --git a/apps/web/src/client/components/ui/layout/SettingsView.tsx b/apps/web/src/client/components/ui/layout/SettingsView.tsx index 2d9cb187..f7d2cdf3 100644 --- a/apps/web/src/client/components/ui/layout/SettingsView.tsx +++ b/apps/web/src/client/components/ui/layout/SettingsView.tsx @@ -12,6 +12,7 @@ import { WindowedModal } from "../WindowedModal"; import { Button } from "../Button"; import { PasskeyModal } from "../PasskeyModal"; import { ErrorModal } from "../ErrorModal"; +import { registerCurrentDevice } from "@/client/encryption"; export function SettingsView({ isOpen, setIsOpen }: { isOpen: boolean, @@ -37,7 +38,17 @@ export function SettingsView({ isOpen, setIsOpen }: { return; (async () => { - setLocalDevices(await deviceStorage.getAllDevices()); + let allDevices = await deviceStorage.getAllDevices(); + + if (!allDevices.some(d => d.thisDevice)) { + try { + await registerCurrentDevice(); + allDevices = await deviceStorage.getAllDevices(); + } + catch {} + } + + setLocalDevices(allDevices); const res = await trpc.user.getDevices.query({}); console.log("(SettingsView.tsx) user.getDevices =", res); diff --git a/apps/web/src/client/encryption.ts b/apps/web/src/client/encryption.ts index c24aa530..b140c3fb 100644 --- a/apps/web/src/client/encryption.ts +++ b/apps/web/src/client/encryption.ts @@ -54,6 +54,26 @@ function generatePasskey() { } } +export async function registerCurrentDevice(): Promise { + const res = await trpc.user.registerDevice.mutate({ + name: platform.description ?? navigator.platform + }); + + if (!res.ok) + throw new Error(res.error); + + const assignedDevice = res.data.device; + const device: LocalDevice = { + id: assignedDevice.id, + passkey: generatePasskey(), + thisDevice: true + }; + + await deviceStorage.saveDevice(device); + await deviceStorage.sync(); + return device; +} + export async function getCurrentDevice(): Promise { const existing = (await deviceStorage.getAllDevices()).find(x => x.thisDevice); if (existing) { @@ -73,22 +93,7 @@ export async function getCurrentDevice(): Promise { } // We haven't registered this device with the server yet! Assign it an ID. - const res = await trpc.user.registerDevice.mutate({ - name: platform.description ?? navigator.platform - }); - - if (!res.ok) - throw new Error(res.error); - - const assignedDevice = res.data.device; - const device: LocalDevice = { - id: assignedDevice.id, - passkey: generatePasskey(), - thisDevice: true - }; - - deviceStorage.saveDevice(device); - return device; + return await registerCurrentDevice(); } export interface KeyIvPair {