diff --git a/src/app/(auth)/login/page.tsx b/src/app/(auth)/login/page.tsx index bf56382..beed76b 100644 --- a/src/app/(auth)/login/page.tsx +++ b/src/app/(auth)/login/page.tsx @@ -1,66 +1,99 @@ "use client"; +import { useState } from "react"; import { authClient } from "@/lib/auth-client"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import { useEffect } from "react"; import { toast } from "sonner"; +import { Card, CardBody } from "@/design-system/base/card"; +import { Input } from "@/design-system/base/input"; +import { PrimaryButton } from "@/design-system/base/button"; +import { VStack } from "@/design-system/layout/stack"; export default function LoginPage() { - const searchParams = useSearchParams(); - const redirectTo = searchParams.get("redirect"); + const [username, setUsername] = useState(""); + const [password, setPassword] = useState(""); + const [loading, setLoading] = useState(false); - const session = authClient.useSession().data; - const router = useRouter(); + const searchParams = useSearchParams(); + const redirectTo = searchParams.get("redirect"); - useEffect(() => { - if (session) { - router.push(redirectTo ?? "/profile"); - } - }); + const session = authClient.useSession().data; + const router = useRouter(); - function login() { - const username = (document.getElementById("username") as HTMLInputElement).value; - const password = (document.getElementById("password") as HTMLInputElement).value; - console.log(username, password); - if (username.includes("@")) { - authClient.signIn.email({ - email: username, - password: username - }); - } else { - authClient.signIn.username({ - username: username, - password: password, - fetchOptions: { - onError: (ctx) => { - toast.error(ctx.error.message); - } - } - }); - } + useEffect(() => { + if (session) { + router.push(redirectTo ?? "/profile"); + } + }, [session, router, redirectTo]); + + const handleLogin = async () => { + if (!username || !password) { + toast.error("请输入用户名和密码"); + return; } - return ( -
-
-

登录

- - - - 没有账号?去注册 -
-
- ); + setLoading(true); + try { + if (username.includes("@")) { + await authClient.signIn.email({ + email: username, + password: username + }); + } else { + await authClient.signIn.username({ + username: username, + password: password, + }); + } + router.push(redirectTo ?? "/profile"); + } catch (error) { + toast.error("登录失败"); + } finally { + setLoading(false); + } + }; + + return ( +
+ + + +

登录

+ + + setUsername(e.target.value)} + /> + + setPassword(e.target.value)} + /> + + + + 确认 + + + + 没有账号?去注册 + +
+
+
+
+ ); } diff --git a/src/app/(auth)/signup/page.tsx b/src/app/(auth)/signup/page.tsx index 3d78235..45ce8ad 100644 --- a/src/app/(auth)/signup/page.tsx +++ b/src/app/(auth)/signup/page.tsx @@ -1,67 +1,102 @@ "use client"; +import { useState } from "react"; import { authClient } from "@/lib/auth-client"; import Link from "next/link"; import { useRouter, useSearchParams } from "next/navigation"; import { useEffect } from "react"; import { toast } from "sonner"; +import { Card, CardBody } from "@/design-system/base/card"; +import { Input } from "@/design-system/base/input"; +import { PrimaryButton } from "@/design-system/base/button"; +import { VStack } from "@/design-system/layout/stack"; export default function SignUpPage() { - const searchParams = useSearchParams(); - const redirectTo = searchParams.get("redirect"); + const [username, setUsername] = useState(""); + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [loading, setLoading] = useState(false); - const session = authClient.useSession().data; - const router = useRouter(); + const searchParams = useSearchParams(); + const redirectTo = searchParams.get("redirect"); - console.log(JSON.stringify({ re: redirectTo })); + const session = authClient.useSession().data; + const router = useRouter(); - useEffect(() => { - if (session) { - router.push(redirectTo ?? "/profile"); - } - }); + useEffect(() => { + if (session) { + router.push(redirectTo ?? "/profile"); + } + }, [session, router, redirectTo]); - function login() { - const username = (document.getElementById("username") as HTMLInputElement).value; - const email = (document.getElementById("email") as HTMLInputElement).value; - const password = (document.getElementById("password") as HTMLInputElement).value; - authClient.signUp.email({ - email: email, - name: username, - username: username, - password: password, - fetchOptions: { - onError: (ctx) => { - toast.error(ctx.error.message); - } - } - }); + const handleSignUp = async () => { + if (!username || !email || !password) { + toast.error("请填写所有字段"); + return; } - return ( -
-
-

注册

- - - - - 已有账号?去登录 -
-
- ); + setLoading(true); + try { + await authClient.signUp.email({ + email: email, + name: username, + username: username, + password: password, + }); + router.push(redirectTo ?? "/profile"); + } catch (error) { + toast.error("注册失败"); + } finally { + setLoading(false); + } + }; + + return ( +
+ + + +

注册

+ + + setUsername(e.target.value)} + /> + + setEmail(e.target.value)} + /> + + setPassword(e.target.value)} + /> + + + + 确认 + + + + 已有账号?去登录 + +
+
+
+
+ ); } diff --git a/src/app/(features)/memorize/page.tsx b/src/app/(features)/memorize/page.tsx index 52a84e1..ce57392 100644 --- a/src/app/(features)/memorize/page.tsx +++ b/src/app/(features)/memorize/page.tsx @@ -24,7 +24,7 @@ export default async function MemorizePage({ if (!folder_id) { const session = await auth.api.getSession({ headers: await headers() }); - if (!session) redirect("/auth?redirect=/memorize"); + if (!session) redirect("/login?redirect=/memorize"); return ( ; } diff --git a/src/modules/auth/auth-action.ts b/src/modules/auth/auth-action.ts index a756fb9..bf9b766 100644 --- a/src/modules/auth/auth-action.ts +++ b/src/modules/auth/auth-action.ts @@ -139,13 +139,13 @@ export async function signOutAction() { headers: await headers() }); - redirect("/auth"); + redirect("/login"); } catch (e) { if (e instanceof Error && e.message.includes('NEXT_REDIRECT')) { throw e; } console.error("Sign out error:", e); - redirect("/auth"); + redirect("/login"); } }