From 4931f6c201e34ef9569d9666085775e26acfb309 Mon Sep 17 00:00:00 2001 From: Pas <74743263+Pasithea0@users.noreply.github.com> Date: Sun, 13 Apr 2025 16:06:44 -0600 Subject: [PATCH] use shorter user ids --- .../migration.sql | 18 ++++++++++ prisma/schema.prisma | 4 +-- server/routes/auth/register/complete.ts | 4 +-- server/utils/id.ts | 34 +++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 prisma/migrations/20250413215708_update_user_id_format/migration.sql create mode 100644 server/utils/id.ts diff --git a/prisma/migrations/20250413215708_update_user_id_format/migration.sql b/prisma/migrations/20250413215708_update_user_id_format/migration.sql new file mode 100644 index 0000000..09f9593 --- /dev/null +++ b/prisma/migrations/20250413215708_update_user_id_format/migration.sql @@ -0,0 +1,18 @@ +/* + Warnings: + + - The primary key for the `user_settings` table will be changed. If it partially fails, the table could be left without primary key constraint. + - You are about to alter the column `id` on the `user_settings` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(8)`. + - The primary key for the `users` table will be changed. If it partially fails, the table could be left without primary key constraint. + - You are about to alter the column `id` on the `users` table. The data in that column could be lost. The data in that column will be cast from `Text` to `VarChar(8)`. + +*/ +-- AlterTable +ALTER TABLE "user_settings" DROP CONSTRAINT "user_settings_pkey", +ALTER COLUMN "id" SET DATA TYPE VARCHAR(8), +ADD CONSTRAINT "user_settings_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "users" DROP CONSTRAINT "users_pkey", +ALTER COLUMN "id" SET DATA TYPE VARCHAR(8), +ADD CONSTRAINT "users_pkey" PRIMARY KEY ("id"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index caa1495..a9a731a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -58,7 +58,7 @@ model sessions { } model user_settings { - id String @id + id String @id @db.VarChar(8) application_theme String? @db.VarChar(255) application_language String? @db.VarChar(255) default_subtitle_language String? @db.VarChar(255) @@ -68,7 +68,7 @@ model user_settings { } model users { - id String @id + id String @id @db.VarChar(8) public_key String @unique(map: "users_public_key_unique") namespace String @db.VarChar(255) created_at DateTime @db.Timestamptz(0) diff --git a/server/routes/auth/register/complete.ts b/server/routes/auth/register/complete.ts index 53ca04f..1f89d18 100644 --- a/server/routes/auth/register/complete.ts +++ b/server/routes/auth/register/complete.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; import { useChallenge } from '~/utils/challenge'; import { useAuth } from '~/utils/auth'; -import { randomUUID } from 'crypto'; +import { generateShortId } from '~/utils/id'; const completeSchema = z.object({ publicKey: z.string(), @@ -49,7 +49,7 @@ export default defineEventHandler(async (event) => { }); } - const userId = randomUUID(); + const userId = generateShortId(); const now = new Date(); const user = await prisma.users.create({ diff --git a/server/utils/id.ts b/server/utils/id.ts new file mode 100644 index 0000000..2e0b936 --- /dev/null +++ b/server/utils/id.ts @@ -0,0 +1,34 @@ +import { randomBytes } from 'crypto'; + +/** + * Generates a short, unique ID for users + * Format: 8 characters (base62 encoded) + * This provides ~218 trillion possible combinations + */ +export function generateShortId(): string { + // Generate 6 random bytes (48 bits) + const bytes = randomBytes(6); + + // Convert to base62 (0-9, a-z, A-Z) + const base62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + let result = ''; + + // Convert bytes to base62 + let value = 0; + for (let i = 0; i < bytes.length; i++) { + value = (value << 8) + bytes[i]; + } + + // Convert to base62 + while (value > 0) { + result = base62[value % 62] + result; + value = Math.floor(value / 62); + } + + // Pad with zeros if needed + while (result.length < 8) { + result = '0' + result; + } + + return result; +} \ No newline at end of file