Files
user-auth/src/index.ts
T
2026-10-11 22:41:04 +08:00

226 lines
5.6 KiB
TypeScript

import { sql, SQL } from "bun";
import { Hono } from "hono";
import { validator } from "hono/validator";
import { checkAdmin } from "./utils/admin";
import { addUser, checkUser, getAllUser } from "./utils/user";
import { responseStr } from "./const/response_str";
import { checkEnv } from "./utils/env";
checkEnv()
const appTmp = new Hono()
const app = appTmp.basePath("/auth")
const db = new SQL({
"adapter": "mariadb",
"host": Bun.env.DB_HOST ?? "localhost",
"user": Bun.env.DB_USER_NAME ?? "root",
"password": Bun.env.DB_USER_PASSWD ?? "root",
"database": Bun.env.DB_NAME ?? "user_auth",
"allowPublicKeyRetrieval": true
})
// 鉴权
app.get(
"/",
validator("query", async (val, c) => {
const name = val.name ?? ""
const passwd = val.passwd ?? ""
// 判空
if (name == null ||
name == "" ||
passwd == null ||
passwd == ""
) {
c.status(401)
return c.json({
"msg": responseStr.REQ_PARAM_ERR
})
}
}),
async (c) => {
const req = c.req.query()
const name = req.name ?? ""
const passwd = req.passwd ?? ""
const re = await checkUser(db, name.toString(), passwd.toString())
if (!re) {
c.status(401)
return c.json({
"msg": responseStr.USER_INVALID
})
}
return c.json({
"code": 200,
"msg": "ok"
})
}
)
// 查询所有用户
app.get(
"/user",
validator("query", async (val, c) => {
const adminName = val.admin_name ?? ""
const adminPasswd = val.admin_passwd ?? ""
// 判空
if (adminName == null ||
adminName == "" ||
adminPasswd == null ||
adminPasswd == ""
) {
c.status(401)
return c.json({
"msg": responseStr.REQ_PARAM_ERR
})
}
// 验证是否是合法管理员
const re = await checkAdmin(db, adminName.toString(), adminPasswd.toString())
if (re) {
return val
} else {
c.status(401)
return c.json({
"msg": responseStr.ADMIN_INVALID
})
}
}),
async (c) => {
const re = await getAllUser(db)
return c.json({
"msg": "ok",
"data": re
})
}
)
// 添加用户
app.post(
"/user",
validator("json", (val, c) => {
if (
val.name == null ||
val.name == "" ||
val.passwd == null ||
val.passwd == ""
) {
c.status(400)
c.status(400)
return c.json({
"code": 400,
"msg": responseStr.REQ_PARAM_ERR
})
} else {
return val
}
}),
async (c) => {
const reqHeader = c.req.query()
const adminName = reqHeader.admin_name ?? ""
const adminPasswd = reqHeader.admin_passwd ?? ""
if (!(await checkAdmin(db, adminName, adminPasswd))) {
c.status(401)
return c.json({
"code": 401,
"msg": responseStr.ADMIN_INVALID
})
}
const req = await c.req.json()
const name: string = req.name ?? ""
const passwd: string = req.passwd ?? ""
if (name.length <= 0 || name.length >= 11) {
c.status(400)
return c.json({
"code": 400,
"msg": responseStr.USER_NAME_LENGTH_LIMIT
})
}
if (passwd.length <= 0 || passwd.length >= 31) {
c.status(400)
return c.json({
"code": 400,
"msg": responseStr.USER_NAME_LENGTH_LIMIT
})
}
const re = await addUser(db, name, passwd)
if (re.state != 0) {
c.status(500)
return c.json({
"code": 500,
"msg": re.msg
})
}
return c.json({
"code": 200,
"msg": "ok"
})
}
)
// 添加管理员
app.post(
"/admin",
validator("json", (val, c) => {
if (
val.name == null ||
val.name == "" ||
val.passwd == null ||
val.passwd == ""
) {
c.status(400)
c.status(400)
return c.json({
"code": 400,
"msg": responseStr.REQ_PARAM_ERR
})
} else {
return val
}
}),
async (c) => {
const req = await c.req.json()
const name: string = req.name ?? ""
const passwd: string = req.passwd ?? ""
if (name.length <= 0 || name.length >= 11) {
c.status(400)
return c.json({
"code": 400,
"msg": responseStr.USER_NAME_LENGTH_LIMIT
})
}
if (passwd.length <= 0 || passwd.length >= 31) {
c.status(400)
return c.json({
"code": 400,
"msg": responseStr.USER_NAME_LENGTH_LIMIT
})
}
const encryptPasswd = await Bun.password.hash(passwd)
try {
await db`insert into admin(name, passwd) values(${name}, ${encryptPasswd})`
} catch (e) {
c.status(500)
return c.json({
"code": 500,
"msg": responseStr.SERVER_INTERNAL_ERR
})
}
return c.json({
"code": 200,
"msg": "ok"
})
}
)
export default {
port: 8080,
fetch: app.fetch
}