|
@@ -3,70 +3,52 @@ import { useState } from "react";
|
|
import Head from "next/head";
|
|
import Head from "next/head";
|
|
import Alert from "../ui/components/Alert";
|
|
import Alert from "../ui/components/Alert";
|
|
import { useRouter } from "next/navigation";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
+import Link from "next/link";
|
|
|
|
|
|
-export default function AuthPage() {
|
|
|
|
|
|
+export default function LoginPage() {
|
|
const router = useRouter();
|
|
const router = useRouter();
|
|
-
|
|
|
|
- const [isLogin, setIsLogin] = useState(true);
|
|
|
|
const [username, setUsername] = useState("");
|
|
const [username, setUsername] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [alert, setAlert] = useState(null);
|
|
const [alert, setAlert] = useState(null);
|
|
|
|
|
|
const handleSubmit = async (e) => {
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
e.preventDefault();
|
|
- const values = { username, password };
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
- const endpoint = isLogin ? "/api/auth/login" : "/api/auth/register";
|
|
|
|
- const response = await fetch(endpoint, {
|
|
|
|
|
|
+ const response = await fetch("/api/auth/login", {
|
|
method: "POST",
|
|
method: "POST",
|
|
headers: {
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"Content-Type": "application/json",
|
|
},
|
|
},
|
|
- body: JSON.stringify(values),
|
|
|
|
|
|
+ body: JSON.stringify({ username, password }),
|
|
});
|
|
});
|
|
|
|
|
|
const res = await response.json();
|
|
const res = await response.json();
|
|
|
|
|
|
if (res.success) {
|
|
if (res.success) {
|
|
- if (isLogin) {
|
|
|
|
- console.log("登录成功", res.user);
|
|
|
|
- setAlert({ type: "success", message: "登录成功" });
|
|
|
|
- localStorage.setItem("currentUser", JSON.stringify(res.user));
|
|
|
|
- setTimeout(() => {
|
|
|
|
- router.push("/");
|
|
|
|
- }, 1000);
|
|
|
|
- } else {
|
|
|
|
- setAlert({ type: "success", message: "注册成功" });
|
|
|
|
- setIsLogin(true);
|
|
|
|
- }
|
|
|
|
|
|
+ setAlert({ type: "success", message: "登录成功" });
|
|
|
|
+ localStorage.setItem("currentUser", JSON.stringify(res.user));
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ router.push("/");
|
|
|
|
+ }, 1000);
|
|
} else {
|
|
} else {
|
|
setAlert({ type: "error", message: res.error });
|
|
setAlert({ type: "error", message: res.error });
|
|
- if (isLogin) {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- router.push("/login");
|
|
|
|
- }, 2000);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- console.error(
|
|
|
|
- "Error during " + (isLogin ? "login" : "registration") + ":",
|
|
|
|
- error
|
|
|
|
- );
|
|
|
|
- setAlert({ type: "error", message: "操作失败,请重试" });
|
|
|
|
|
|
+ console.error("Login error:", error);
|
|
|
|
+ setAlert({ type: "error", message: "登录失败,请重试" });
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
return (
|
|
return (
|
|
<div className="min-h-screen bg-gray-100 flex flex-col justify-center px-4 sm:px-6 lg:px-8">
|
|
<div className="min-h-screen bg-gray-100 flex flex-col justify-center px-4 sm:px-6 lg:px-8">
|
|
<Head>
|
|
<Head>
|
|
- <title>{isLogin ? "登录" : "注册"}</title>
|
|
|
|
- <meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
|
|
|
|
+ <title>登录</title>
|
|
</Head>
|
|
</Head>
|
|
|
|
|
|
<div className="w-full max-w-md mx-auto">
|
|
<div className="w-full max-w-md mx-auto">
|
|
<h2 className="mt-6 text-center text-2xl sm:text-3xl font-extrabold text-gray-900">
|
|
<h2 className="mt-6 text-center text-2xl sm:text-3xl font-extrabold text-gray-900">
|
|
- {isLogin ? "登录您的账户" : "创建新账户"}
|
|
|
|
|
|
+ 登录您的账户
|
|
</h2>
|
|
</h2>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
@@ -80,17 +62,14 @@ export default function AuthPage() {
|
|
>
|
|
>
|
|
用户名
|
|
用户名
|
|
</label>
|
|
</label>
|
|
- <div className="mt-1">
|
|
|
|
- <input
|
|
|
|
- id="username"
|
|
|
|
- name="username"
|
|
|
|
- autoComplete="tel"
|
|
|
|
- required
|
|
|
|
- className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 text-base"
|
|
|
|
- value={username}
|
|
|
|
- onChange={(e) => setUsername(e.target.value)}
|
|
|
|
- />
|
|
|
|
- </div>
|
|
|
|
|
|
+ <input
|
|
|
|
+ id="username"
|
|
|
|
+ type="text"
|
|
|
|
+ required
|
|
|
|
+ className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
|
|
|
|
+ value={username}
|
|
|
|
+ onChange={(e) => setUsername(e.target.value)}
|
|
|
|
+ />
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<div>
|
|
<div>
|
|
@@ -100,28 +79,22 @@ export default function AuthPage() {
|
|
>
|
|
>
|
|
密码
|
|
密码
|
|
</label>
|
|
</label>
|
|
- <div className="mt-1">
|
|
|
|
- <input
|
|
|
|
- id="password"
|
|
|
|
- name="password"
|
|
|
|
- type="password"
|
|
|
|
- autoComplete="current-password"
|
|
|
|
- required
|
|
|
|
- className="appearance-none block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 text-base"
|
|
|
|
- value={password}
|
|
|
|
- onChange={(e) => setPassword(e.target.value)}
|
|
|
|
- />
|
|
|
|
- </div>
|
|
|
|
|
|
+ <input
|
|
|
|
+ id="password"
|
|
|
|
+ type="password"
|
|
|
|
+ required
|
|
|
|
+ className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500"
|
|
|
|
+ value={password}
|
|
|
|
+ onChange={(e) => setPassword(e.target.value)}
|
|
|
|
+ />
|
|
</div>
|
|
</div>
|
|
|
|
|
|
- <div>
|
|
|
|
- <button
|
|
|
|
- type="submit"
|
|
|
|
- className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-base font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
|
|
|
- >
|
|
|
|
- {isLogin ? "登录" : "注册"}
|
|
|
|
- </button>
|
|
|
|
- </div>
|
|
|
|
|
|
+ <button
|
|
|
|
+ type="submit"
|
|
|
|
+ className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-base font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
|
|
|
+ >
|
|
|
|
+ 登录
|
|
|
|
+ </button>
|
|
</form>
|
|
</form>
|
|
|
|
|
|
<div className="mt-6">
|
|
<div className="mt-6">
|
|
@@ -130,19 +103,23 @@ export default function AuthPage() {
|
|
<div className="w-full border-t border-gray-300" />
|
|
<div className="w-full border-t border-gray-300" />
|
|
</div>
|
|
</div>
|
|
<div className="relative flex justify-center text-sm">
|
|
<div className="relative flex justify-center text-sm">
|
|
- <span className="px-2 bg-white text-gray-500">
|
|
|
|
- {isLogin ? "还没有账户?" : "已经有账户?"}
|
|
|
|
- </span>
|
|
|
|
|
|
+ <span className="px-2 bg-white text-gray-500">或者</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
- <div className="mt-6">
|
|
|
|
- <button
|
|
|
|
- onClick={() => setIsLogin(!isLogin)}
|
|
|
|
|
|
+ <div className="mt-6 space-y-4">
|
|
|
|
+ <Link
|
|
|
|
+ href="/register"
|
|
|
|
+ className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-base font-medium text-indigo-600 bg-white hover:bg-gray-50"
|
|
|
|
+ >
|
|
|
|
+ 创建新账户
|
|
|
|
+ </Link>
|
|
|
|
+ <Link
|
|
|
|
+ href="/reset-password"
|
|
className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-base font-medium text-indigo-600 bg-white hover:bg-gray-50"
|
|
className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-base font-medium text-indigo-600 bg-white hover:bg-gray-50"
|
|
>
|
|
>
|
|
- {isLogin ? "创建新账户" : "登录已有账户"}
|
|
|
|
- </button>
|
|
|
|
|
|
+ 忘记密码?
|
|
|
|
+ </Link>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|