/* * @Date: 2025-06-11 17:57:58 * @LastEditors: 陈子健 * @LastEditTime: 2025-06-13 13:50:14 * @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) => { console.log(c.req.json(), 111) const { username, password } = await c.req.json() // 从数据库验证用户 const user = await 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({ code: 200, data: { token }, message: '登录成功' }) } return c.json({ code: 401, message: '用户名或密码错误' }, 401) }) export default user