Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(migrate): 添加管理员用户创建的环境变量,移除迁移文件中的默认用户

Timothy Yinb4c4706ade3c
diff --git a/.env.example b/.env.example
index c20de552..ace839b7 100644
--- a/.env.example
+++ b/.env.example
@@ -1,3 +1,10 @@
+# Admin user email (required)
+CFRAME_ADMIN_EMAIL=
+# Admin user name (default to Chronoframe, optional)
+CFRAME_ADMIN_NAME=
+# Admin user password (default to CF1234@!, optional)
+CFRAME_ADMIN_PASSWORD=
+
# Site configuration
NUXT_PUBLIC_APP_TITLE=ChronoFrame
NUXT_PUBLIC_APP_SLOGAN=Mems rest within the lens.
diff --git a/README.md b/README.md
index c6b0b1a9..46b07729 100644
--- a/README.md
+++ b/README.md
@@ -49,6 +49,9 @@ docker run -d \
--name chronoframe \
-p 3000:3000 \
-v $(pwd)/data:/app/data \
+ -e CFRAME_ADMIN_EMAIL="" \
+ -e CFRAME_ADMIN_NAME="" \
+ -e CFRAME_ADMIN_PASSWORD="" \
-e NUXT_PUBLIC_APP_TITLE="" \
-e NUXT_PUBLIC_APP_SLOGAN="" \
-e NUXT_PUBLIC_APP_AVATAR_URL="" \
@@ -72,6 +75,13 @@ docker run -d \
创建 `.env` 文件
```env
+# Admin user email (required)
+CFRAME_ADMIN_EMAIL=
+# Admin user name (default to Chronoframe, optional)
+CFRAME_ADMIN_NAME=
+# Admin user password (default to CF1234@!, optional)
+CFRAME_ADMIN_PASSWORD=
+
# 应用标题与口号
NUXT_PUBLIC_APP_TITLE=
NUXT_PUBLIC_APP_SLOGAN=
@@ -124,6 +134,9 @@ docker-compose up -d
| 环境变量 | 说明 | 默认值 | 必需 |
| :--------------------------------- | :--------------------------------------- | :---------- | :---------------------------------------- |
+| CFRAME_ADMIN_EMAIL | 初始管理员用户的邮箱 | 无 | 是,填写登录使用的 GitHub 账户的邮箱 |
+| CFRAME_ADMIN_NAME | 初始管理员用户的用户名 | Chronoframe | 否 |
+| CFRAME_ADMIN_PASSWORD | 初始管理员用户的密码 | CF1234@! | 否 |
| NUXT_PUBLIC_APP_TITLE | 应用标题 | ChronoFrame | 否 |
| NUXT_PUBLIC_APP_SLOGAN | 应用口号 | 无 | 否 |
| NUXT_PUBLIC_APP_AVATAR_URL | 应用头像 URL | 无 | 否 |
@@ -176,7 +189,6 @@ cp .env.example .env
### 数据库初始化
```bash
-# 1. 在 /server/database/migrations/0000_curvy_scarlet_witch.sql 中修改初始账号
# 2. 生成数据库迁移文件(可选)
pnpm db:generate
diff --git a/package.json b/package.json
index cf0c8208..a1675bbe 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,8 @@
"private": true,
"scripts": {
"db": "drizzle-kit",
- "db:migrate": "drizzle-kit migrate",
+ "db:migrate": "node scripts/migrate.mjs",
+ "db:migrate-drizzle": "drizzle-kit migrate",
"db:generate": "drizzle-kit generate",
"build": "nuxt build",
"build:deps": "pnpm --filter @chronoframe/webgl-image build",
diff --git a/scripts/migrate.mjs b/scripts/migrate.mjs
index 38c481fd..89324712 100644
--- a/scripts/migrate.mjs
+++ b/scripts/migrate.mjs
@@ -4,6 +4,38 @@ import { drizzle } from 'drizzle-orm/better-sqlite3'
import Database from 'better-sqlite3'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
+import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
+import { scrypt, randomBytes } from 'node:crypto'
+import { promisify } from 'node:util'
+
+const scryptAsync = promisify(scrypt)
+
+/**
+ * 基于 @adonisjs/hash 的 Scrypt 驱动程序兼容实现
+ */
+async function hashPassword(password) {
+ const options = {
+ cost: 16384, // N parameter (CPU/memory cost)
+ blockSize: 8, // r parameter (block size)
+ parallelization: 1, // p parameter (parallelization factor)
+ keyLength: 64, // derived key length
+ saltSize: 16, // salt size in bytes
+ }
+
+ const salt = randomBytes(options.saltSize)
+
+ const derivedKey = await scryptAsync(password, salt, options.keyLength, {
+ cost: options.cost,
+ blockSize: options.blockSize,
+ parallelization: options.parallelization,
+ })
+
+ // $scrypt$n=16384,r=8,p=1$salt$hash
+ const saltBase64 = salt.toString('base64')
+ const hashBase64 = derivedKey.toString('base64')
+
+ return `$scrypt$n=${options.cost},r=${options.blockSize},p=${options.parallelization}$${saltBase64}$${hashBase64}`
+}
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
@@ -13,11 +45,41 @@ console.log('Running database migrations...')
try {
const sqlite = new Database(process.env.DATABASE_URL || './data/app.sqlite3')
const db = drizzle(sqlite)
-
- await migrate(db, {
- migrationsFolder: join(__dirname, '../server/database/migrations')
+
+ migrate(db, {
+ migrationsFolder: join(__dirname, '../server/database/migrations'),
+ })
+
+ const usersTable = sqliteTable('users', {
+ id: integer('id').primaryKey({ autoIncrement: true }),
+ username: text('name').notNull().unique(),
+ email: text('email').notNull().unique(),
+ password: text('password'),
+ avatar: text('avatar'),
+ createdAt: integer('created_at', { mode: 'timestamp' }).notNull(),
+ isAdmin: integer('is_admin').default(0).notNull(),
})
-
+
+ const existingUsers = await db.select().from(usersTable)
+
+ if (existingUsers.length === 0) {
+ console.log('No users found, creating admin user...')
+ const hashedPassword = await hashPassword(
+ process.env.CFRAME_ADMIN_PASSWORD || 'admin',
+ )
+
+ await db.insert(usersTable).values({
+ username: process.env.CFRAME_ADMIN_NAME || 'Chronoframe',
+ email: process.env.CFRAME_ADMIN_EMAIL || '[email protected]', // This default email does not exist
+ password: hashedPassword,
+ createdAt: new Date(),
+ isAdmin: 1,
+ })
+ console.log('Admin user created successfully.')
+ } else {
+ console.log('User already exists, skipping admin user creation.')
+ }
+
console.log('Database migrations completed successfully!')
sqlite.close()
} catch (error) {
diff --git a/server/database/migrations/0000_curvy_scarlet_witch.sql b/server/database/migrations/0000_curvy_scarlet_witch.sql
index 5e8a5b9f..c42fa8b4 100644
--- a/server/database/migrations/0000_curvy_scarlet_witch.sql
+++ b/server/database/migrations/0000_curvy_scarlet_witch.sql
@@ -28,8 +28,4 @@ CREATE TABLE `users` (
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_name_unique` ON `users` (`name`);--> statement-breakpoint
-CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);
-
---> statement-breakpoint
-INSERT INTO users (name, email, password, avatar, created_at, is_admin) VALUES
- ('HoshinoSuzumi', '[email protected]', '$scrypt$n=16384,r=8,p=1$QaLKQI/LB+Kh7jgyeH4lGw$NuoxPwPvDZGK3F9oTNEgUNMAi24z/Wei6hWnJzNdscwKDHpQQVJfL1BlcNC+gHJtdJn4YsmQ9tWbVOCE4RgL1g', NULL, strftime('%s', 'now'), 1);
\ No newline at end of file
+CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);
\ No newline at end of file