From d6b753e405b1d6b3cedf209a502240705bc8643d Mon Sep 17 00:00:00 2001 From: CancerAi Date: Mon, 12 Oct 2026 11:28:34 +0800 Subject: [PATCH] =?UTF-8?q?fea:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=A0=B9=E6=8D=AE=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AE=A1=E7=90=86=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 3 ++- src/utils/admin.ts | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index e5116e0..3eace1f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { sql, SQL } from "bun"; import { Hono } from "hono"; import { validator } from "hono/validator"; -import { checkAdmin } from "./utils/admin"; +import { checkAdmin, initAdmin } from "./utils/admin"; import { addUser, checkUser, getAllUser } from "./utils/user"; import { responseStr } from "./const/response_str"; import { checkEnv } from "./utils/env"; @@ -17,6 +17,7 @@ const db = new SQL({ "database": Bun.env.DB_NAME ?? "user_auth", "allowPublicKeyRetrieval": true }) +await initAdmin(db) // 鉴权 app.get( diff --git a/src/utils/admin.ts b/src/utils/admin.ts index 95fc76f..49ed763 100644 --- a/src/utils/admin.ts +++ b/src/utils/admin.ts @@ -14,4 +14,16 @@ export async function checkAdmin( return false } return await Bun.password.verify(passwd, rows[0]!["passwd"]) +} + +export async function initAdmin(db: SQL) { + const admin_name = Bun.env.admin_name ?? "" + const admin_passwd = Bun.env.admin_passwd ?? "" + // 判断有没有管理员 + const adminRows = await db`select * from admin` + if (adminRows.length != 0) { + return + } + const encryptPasswd = await Bun.password.hash(admin_passwd) + await db`insert into admin(name, passwd) values(${admin_name}, ${encryptPasswd})` } \ No newline at end of file