Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion apps/web/src/client/components/ui/layout/SettingsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
37 changes: 21 additions & 16 deletions apps/web/src/client/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ function generatePasskey() {
}
}

export async function registerCurrentDevice(): Promise<LocalDevice> {
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<LocalDevice> {
const existing = (await deviceStorage.getAllDevices()).find(x => x.thisDevice);
if (existing) {
Expand All @@ -73,22 +93,7 @@ export async function getCurrentDevice(): Promise<LocalDevice> {
}

// 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 {
Expand Down