fix(backend): remove hardwareId references, use deviceId only

This commit is contained in:
chahinebrini 2026-06-18 00:06:39 +02:00
parent a60def33d9
commit 943efe4b45
3 changed files with 16 additions and 69 deletions

View File

@ -32,7 +32,6 @@ export default defineEventHandler(async (event) => {
select: {
id: true,
deviceId: true,
hardwareId: true,
platform: true,
model: true,
name: true,
@ -66,7 +65,6 @@ export default defineEventHandler(async (event) => {
return {
source: "magic" as const,
deviceId: d.deviceId,
hardwareId: d.hardwareId,
hostname: d.hostname ?? "Unbenanntes Ger\u00e4t",
model: d.model,
osVersion: d.osVersion,
@ -90,7 +88,6 @@ export default defineEventHandler(async (event) => {
return {
source: "locked" as const,
deviceId: d.deviceId,
hardwareId: d.hardwareId,
hostname: d.name ?? d.model ?? prettyPlatform(d.platform),
model: d.model,
osVersion: d.osVersion,

View File

@ -22,9 +22,8 @@ import { generateRemovalPassword } from "../../utils/magic-lock";
export default defineEventHandler(async (event) => {
const user = await requireUser(event);
const body = await readBody(event);
const { deviceId, hardwareId, hostname, model, osVersion, platform } = body as {
const { deviceId, hostname, model, osVersion, platform } = body as {
deviceId?: string;
hardwareId?: string;
hostname?: string;
model?: string;
osVersion?: string;
@ -38,16 +37,14 @@ export default defineEventHandler(async (event) => {
});
}
if (!deviceId && !hardwareId) {
if (!deviceId) {
throw createError({
statusCode: 400,
message: "deviceId oder hardwareId required",
message: "deviceId required",
});
}
// Für neue Magic-Registrierungen: hardwareId wird gleichzeitig deviceId,
// damit das Backend keine eigene ID generieren muss.
const effectiveDeviceId = deviceId?.trim() || hardwareId!.trim();
const effectiveDeviceId = deviceId.trim();
// Plattform: Mac-App sendet nichts (legacy default), Windows-App sendet "windows"
const devicePlatform =
@ -56,60 +53,18 @@ export default defineEventHandler(async (event) => {
const db = usePrisma();
// 1. Prüfe ob Device bereits als Magic-Client gebunden ist (idempotent)
// Priorität: hardwareId → deviceId → Migration über Modell/Plattform/OS.
let existing = null;
if (hardwareId) {
existing = await db.userDevice.findFirst({
where: { userId: user.id, hardwareId },
select: {
id: true,
userId: true,
deviceId: true,
magicDnsToken: true,
magicEnrolledAt: true,
magicRevokedAt: true,
magicRemovalPassword: true,
},
});
}
if (!existing && deviceId) {
existing = await db.userDevice.findUnique({
where: { userId_deviceId: { userId: user.id, deviceId } },
select: {
id: true,
userId: true,
deviceId: true,
magicDnsToken: true,
magicEnrolledAt: true,
magicRevokedAt: true,
magicRemovalPassword: true,
},
});
}
// Migration: bestehendes Gerät ohne hardwareId anhand von Modell/Plattform/OS finden.
if (!existing && hardwareId) {
existing = await db.userDevice.findFirst({
where: {
userId: user.id,
hardwareId: null,
platform: devicePlatform,
model: model ?? null,
osVersion: osVersion ?? null,
},
select: {
id: true,
userId: true,
deviceId: true,
magicDnsToken: true,
magicEnrolledAt: true,
magicRevokedAt: true,
magicRemovalPassword: true,
},
});
}
let existing = await db.userDevice.findUnique({
where: { userId_deviceId: { userId: user.id, deviceId } },
select: {
id: true,
userId: true,
deviceId: true,
magicDnsToken: true,
magicEnrolledAt: true,
magicRevokedAt: true,
magicRemovalPassword: true,
},
});
// Wenn Token existiert und nicht revoked → return existing
if (
@ -199,14 +154,12 @@ export default defineEventHandler(async (event) => {
model: model ?? null,
name: hostname,
osVersion: osVersion ?? null,
hardwareId: hardwareId ?? null,
magicDnsToken: dnsToken,
magicEnrolledAt: new Date(),
magicHostname: hostname,
magicRemovalPassword: removalPassword,
},
update: {
hardwareId: hardwareId ?? undefined,
magicDnsToken: dnsToken,
magicEnrolledAt: new Date(),
magicRevokedAt: null, // Clear falls vorher revoked

View File

@ -418,7 +418,6 @@ export async function deleteUserDevice(
export interface MagicDeviceRecord {
deviceId: string;
hardwareId: string | null;
hostname: string | null;
model: string | null;
osVersion: string | null;
@ -447,7 +446,6 @@ export async function listMagicDevices(
orderBy: { magicEnrolledAt: "desc" },
select: {
deviceId: true,
hardwareId: true,
magicHostname: true,
model: true,
osVersion: true,
@ -462,7 +460,6 @@ export async function listMagicDevices(
return devices.map((d) => ({
deviceId: d.deviceId,
hardwareId: d.hardwareId,
hostname: d.magicHostname,
model: d.model,
osVersion: d.osVersion,