迁移数据库从 SQLite 到 PostgreSQL
- 更新 deno.json 添加 postgres 依赖 - 重构 db/index.ts 使用 PostgreSQL 连接和适配器 - 更新所有路由文件支持异步数据库操作 - 将 SQLite 语法转换为 PostgreSQL 语法 - 添加数据库迁移文档和 schema 文件
This commit is contained in:
@@ -4,36 +4,40 @@
|
||||
* @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'
|
||||
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 user = new Hono<{ Variables: JwtVariables }>();
|
||||
|
||||
const AUTH_SECRET = 'it-is-a-secret'
|
||||
const AUTH_SECRET = "it-is-a-secret";
|
||||
|
||||
// 登录路由
|
||||
user.post('/login', async (c) => {
|
||||
const { username, password } = await c.req.json()
|
||||
|
||||
user.post("/login", async (c) => {
|
||||
const { username, password } = await c.req.json();
|
||||
|
||||
// 从数据库验证用户
|
||||
const user = db.prepare('SELECT * FROM users WHERE username = ? AND password = ?')
|
||||
.get(username, password)
|
||||
|
||||
if (user) {
|
||||
const token = await sign({ username: user.username }, AUTH_SECRET)
|
||||
return c.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: '登录成功'
|
||||
})
|
||||
message: "登录成功",
|
||||
});
|
||||
}
|
||||
|
||||
return c.json({
|
||||
code: 401,
|
||||
message: '用户名或密码错误'
|
||||
}, 401)
|
||||
})
|
||||
|
||||
export default user
|
||||
return c.json(
|
||||
{
|
||||
code: 401,
|
||||
message: "用户名或密码错误",
|
||||
},
|
||||
401
|
||||
);
|
||||
});
|
||||
|
||||
export default user;
|
||||
|
||||
Reference in New Issue
Block a user