page.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * 认证页面 - 登录/注册
  3. */
  4. 'use client';
  5. import { useState } from 'react';
  6. import { useAuth } from '@/lib/hooks';
  7. import { useRouter } from 'next/navigation';
  8. export default function AuthPage() {
  9. const [isLogin, setIsLogin] = useState(true);
  10. const [email, setEmail] = useState('');
  11. const [username, setUsername] = useState('');
  12. const [password, setPassword] = useState('');
  13. const [error, setError] = useState('');
  14. const { login, register, isLoading } = useAuth();
  15. const router = useRouter();
  16. const handleSubmit = async (e: React.FormEvent) => {
  17. e.preventDefault();
  18. setError('');
  19. try {
  20. console.log('[Auth] Submitting:', { isLogin, email });
  21. if (isLogin) {
  22. const response = await login(email, password);
  23. console.log('[Auth] Login response:', response);
  24. } else {
  25. const response = await register(email, username, password);
  26. console.log('[Auth] Register response:', response);
  27. }
  28. console.log('[Auth] Redirecting to home...');
  29. router.push('/');
  30. } catch (err) {
  31. console.error('[Auth] Error:', err);
  32. setError(err instanceof Error ? err.message : '操作失败');
  33. }
  34. };
  35. return (
  36. <div className="min-h-screen flex items-center justify-center bg-gray-50 dark:bg-gray-900 px-4">
  37. <div className="max-w-md w-full bg-white dark:bg-gray-800 rounded-lg shadow-md p-8">
  38. <h1 className="text-2xl font-bold text-center mb-6 text-gray-800 dark:text-white">
  39. {isLogin ? '登录' : '注册'} Novel Platform
  40. </h1>
  41. {error && (
  42. <div className="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
  43. {error}
  44. </div>
  45. )}
  46. <form onSubmit={handleSubmit} className="space-y-4">
  47. <div>
  48. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
  49. 邮箱
  50. </label>
  51. <input
  52. type="email"
  53. value={email}
  54. onChange={(e) => setEmail(e.target.value)}
  55. required
  56. className="w-full px-3 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white"
  57. />
  58. </div>
  59. {!isLogin && (
  60. <div>
  61. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
  62. 用户名
  63. </label>
  64. <input
  65. type="text"
  66. value={username}
  67. onChange={(e) => setUsername(e.target.value)}
  68. required
  69. className="w-full px-3 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white"
  70. />
  71. </div>
  72. )}
  73. <div>
  74. <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">
  75. 密码
  76. </label>
  77. <input
  78. type="password"
  79. value={password}
  80. onChange={(e) => setPassword(e.target.value)}
  81. required
  82. className="w-full px-3 py-2 border dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 dark:bg-gray-700 dark:text-white"
  83. />
  84. </div>
  85. <button
  86. type="submit"
  87. disabled={isLoading}
  88. className="w-full py-2 px-4 bg-blue-500 text-white rounded-lg hover:bg-blue-600 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors"
  89. >
  90. {isLoading ? '处理中...' : isLogin ? '登录' : '注册'}
  91. </button>
  92. </form>
  93. <div className="mt-4 text-center">
  94. <button
  95. type="button"
  96. onClick={() => setIsLogin(!isLogin)}
  97. className="text-blue-500 hover:text-blue-600 text-sm"
  98. >
  99. {isLogin ? '没有账号?去注册' : '已有账号?去登录'}
  100. </button>
  101. </div>
  102. </div>
  103. </div>
  104. );
  105. }