- 更新 deno.json 添加 postgres 依赖 - 重构 db/index.ts 使用 PostgreSQL 连接和适配器 - 更新所有路由文件支持异步数据库操作 - 将 SQLite 语法转换为 PostgreSQL 语法 - 添加数据库迁移文档和 schema 文件
44 lines
989 B
TypeScript
44 lines
989 B
TypeScript
/*
|
|
* @Date: 2025-06-11 17:57:58
|
|
* @LastEditors: 陈子健
|
|
* @LastEditTime: 2025-06-16 11:17:09
|
|
* @FilePath: /my-score/honoback/routes/user.ts
|
|
*/
|
|
import { Hono } from "hono";
|
|
import { sign } from "hono/jwt";
|
|
import type { JwtVariables } from "hono/jwt";
|
|
import { db } from "../db/index.ts";
|
|
|
|
const user = new Hono<{ Variables: JwtVariables }>();
|
|
|
|
const AUTH_SECRET = "it-is-a-secret";
|
|
|
|
// 登录路由
|
|
user.post("/login", async (c) => {
|
|
const { username, password } = await c.req.json();
|
|
|
|
// 从数据库验证用户
|
|
const userRecord = await db
|
|
.prepare("SELECT * FROM users WHERE username = ? AND password = ?")
|
|
.get(username, password);
|
|
|
|
if (userRecord) {
|
|
const token = await sign({ username: userRecord.username }, AUTH_SECRET);
|
|
return c.json({
|
|
code: 200,
|
|
data: { token },
|
|
message: "登录成功",
|
|
});
|
|
}
|
|
|
|
return c.json(
|
|
{
|
|
code: 401,
|
|
message: "用户名或密码错误",
|
|
},
|
|
401
|
|
);
|
|
});
|
|
|
|
export default user;
|