2
0

routes_auth.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. return c.json({
  65. message: '登录成功',
  66. token: result.token,
  67. refreshToken: result.refreshToken,
  68. user: result.user
  69. })
  70. } catch (authError) {
  71. return c.json({ error: '用户名或密码错误' }, 401)
  72. }
  73. } catch (error) {
  74. console.error('登录失败:', error)
  75. return c.json({ error: '登录失败' }, 500)
  76. }
  77. })
  78. // 获取当前用户信息
  79. authRoutes.get('/me', withAuth, (c) => {
  80. const user = c.get('user')
  81. return c.json(user)
  82. })
  83. // 登出
  84. authRoutes.post('/logout', async (c) => {
  85. return c.json({ message: '登出成功' })
  86. })
  87. return authRoutes
  88. }