mirror of
https://github.com/p-stream/backend.git
synced 2026-01-11 20:10:33 +00:00
use shorter user ids
This commit is contained in:
parent
dbd11a5afb
commit
4931f6c201
4 changed files with 56 additions and 4 deletions
|
|
@ -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");
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
34
server/utils/id.ts
Normal file
34
server/utils/id.ts
Normal file
|
|
@ -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;
|
||||
}
|
||||
Loading…
Reference in a new issue