101 lines
2.9 KiB
TypeScript
101 lines
2.9 KiB
TypeScript
"use client";
|
||
|
||
import { useState } from "react";
|
||
import { signIn } from "next-auth/react";
|
||
import { useRouter } from "next/navigation";
|
||
import Link from "next/link";
|
||
import { Button } from "@/components/ui/button";
|
||
import { Input } from "@/components/ui/input";
|
||
import { Label } from "@/components/ui/label";
|
||
import {
|
||
Card,
|
||
CardContent,
|
||
CardDescription,
|
||
CardFooter,
|
||
CardHeader,
|
||
CardTitle,
|
||
} from "@/components/ui/card";
|
||
|
||
export default function LoginPage() {
|
||
const router = useRouter();
|
||
const [email, setEmail] = useState("");
|
||
const [password, setPassword] = useState("");
|
||
const [error, setError] = useState("");
|
||
const [loading, setLoading] = useState(false);
|
||
|
||
const handleSubmit = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
setLoading(true);
|
||
setError("");
|
||
const result = await signIn("credentials", {
|
||
email,
|
||
password,
|
||
redirect: false,
|
||
});
|
||
setLoading(false);
|
||
if (result?.error) {
|
||
setError("邮箱或密码错误");
|
||
} else {
|
||
router.push("/dashboard");
|
||
router.refresh();
|
||
}
|
||
};
|
||
|
||
return (
|
||
<Card className="w-full max-w-md">
|
||
<CardHeader className="space-y-1">
|
||
<CardTitle className="text-2xl">登录</CardTitle>
|
||
<CardDescription>请输入您的账号信息登录GEO平台</CardDescription>
|
||
</CardHeader>
|
||
<form onSubmit={handleSubmit}>
|
||
<CardContent className="space-y-4">
|
||
{error && (
|
||
<p className="text-sm text-destructive">{error}</p>
|
||
)}
|
||
<div className="space-y-2">
|
||
<Label htmlFor="email">邮箱</Label>
|
||
<Input
|
||
id="email"
|
||
type="email"
|
||
placeholder="name@example.com"
|
||
value={email}
|
||
onChange={(e) => setEmail(e.target.value)}
|
||
required
|
||
/>
|
||
</div>
|
||
<div className="space-y-2">
|
||
<div className="flex items-center justify-between">
|
||
<Label htmlFor="password">密码</Label>
|
||
<Link
|
||
href="/forgot-password"
|
||
className="text-sm text-blue-600 hover:underline"
|
||
>
|
||
忘记密码?
|
||
</Link>
|
||
</div>
|
||
<Input
|
||
id="password"
|
||
type="password"
|
||
placeholder="请输入密码"
|
||
value={password}
|
||
onChange={(e) => setPassword(e.target.value)}
|
||
required
|
||
/>
|
||
</div>
|
||
</CardContent>
|
||
<CardFooter className="flex flex-col space-y-4">
|
||
<Button type="submit" className="w-full" disabled={loading}>
|
||
{loading ? "登录中..." : "登录"}
|
||
</Button>
|
||
<p className="text-sm text-muted-foreground">
|
||
还没有账号?{" "}
|
||
<Link href="/register" className="text-primary hover:underline">
|
||
立即注册
|
||
</Link>
|
||
</p>
|
||
</CardFooter>
|
||
</form>
|
||
</Card>
|
||
);
|
||
}
|