/* * @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;