routes_auth.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { Hono } from 'hono'
  2. import type { Variables, WithAuth } from "./middlewares.ts";
  3. export function createAuthRoutes(withAuth: WithAuth) {
  4. const authRoutes = new Hono<{ Variables: Variables }>()
  5. // 登录状态检查
  6. authRoutes.get('/status', async (c) => {
  7. try {
  8. const auth = c.get('auth')
  9. const token = c.req.header('Authorization')?.replace('Bearer ', '')
  10. if (!token) {
  11. return c.json({ isValid: false }, 200)
  12. }
  13. const status = await auth.checkLoginStatus(token)
  14. return c.json(status)
  15. } catch (error) {
  16. console.error('登录状态检查失败:', error)
  17. return c.json({ isValid: false, error: '登录状态检查失败' }, 500)
  18. }
  19. })
  20. // 注册
  21. authRoutes.post('/register', async (c) => {
  22. try {
  23. const auth = c.get('auth')
  24. const { username, email, password } = await c.req.json()
  25. if (!username || !password) {
  26. return c.json({ error: '用户名和密码不能为空' }, 400)
  27. }
  28. try {
  29. await auth.createUser({ username, password, email })
  30. const result = await auth.authenticate(username, password)
  31. return c.json({
  32. message: '注册成功',
  33. user: result.user
  34. }, 201)
  35. } catch (authError) {
  36. return c.json({ error: '用户已存在或注册失败' }, 400)
  37. }
  38. } catch (error) {
  39. console.error('注册失败:', error)
  40. return c.json({ error: '注册失败' }, 500)
  41. }
  42. })
  43. // 登录
  44. authRoutes.post('/login', async (c) => {
  45. try {
  46. const auth = c.get('auth')
  47. const { username, password, latitude, longitude } = await c.req.json()
  48. if (!username || !password) {
  49. return c.json({ error: '用户名和密码不能为空' }, 400)
  50. }
  51. try {
  52. const result = await auth.authenticate(username, password)
  53. if (result.user) {
  54. const apiClient = c.get('apiClient')
  55. await apiClient.database.insert('login_history', {
  56. user_id: result.user.id,
  57. login_time: apiClient.database.fn.now(),
  58. ip_address: c.req.header('x-forwarded-for') || '未知',
  59. user_agent: c.req.header('user-agent') || '未知',
  60. latitude: latitude || null,
  61. longitude: longitude || null
  62. })
  63. // 实时查询当前用户的角色
  64. const userRole = await apiClient.database.table('users')
  65. .where('id', result.user.id)
  66. .select('role')
  67. .first()
  68. result.user.role = userRole.role;
  69. }
  70. return c.json({
  71. message: '登录成功',
  72. token: result.token,
  73. refreshToken: result.refreshToken,
  74. user: result.user
  75. })
  76. } catch (authError) {
  77. return c.json({ error: '用户名或密码错误' }, 401)
  78. }
  79. } catch (error) {
  80. console.error('登录失败:', error)
  81. return c.json({ error: '登录失败' }, 500)
  82. }
  83. })
  84. // 获取当前用户信息
  85. authRoutes.get('/me', withAuth, (c) => {
  86. const user = c.get('user')
  87. return c.json(user)
  88. })
  89. // 登出
  90. authRoutes.post('/logout', async (c) => {
  91. return c.json({ message: '登出成功' })
  92. })
  93. return authRoutes
  94. }