page.tsx124 lines, 3.6 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { FaGithub } from "react-icons/fa"; |
| 4 | import { useState } from "react"; |
| 5 | import Link from "next/link"; |
| 6 | import { useRouter } from "next/navigation"; |
| 7 | import { signIn } from "@/lib/auth-client"; |
| 8 | import { Button } from "@/components/ui/button"; |
| 9 | import { Input } from "@/components/ui/input"; |
| 10 | import { Label } from "@/components/ui/label"; |
| 11 | import { |
| 12 | Card, |
| 13 | CardContent, |
| 14 | CardDescription, |
| 15 | CardHeader, |
| 16 | CardTitle, |
| 17 | } from "@/components/ui/card"; |
| 18 | import { Separator } from "@/components/ui/separator"; |
| 19 | |
| 20 | export default function SignInPage() { |
| 21 | const router = useRouter(); |
| 22 | const [email, setEmail] = useState(""); |
| 23 | const [password, setPassword] = useState(""); |
| 24 | const [error, setError] = useState(""); |
| 25 | const [loading, setLoading] = useState(false); |
| 26 | |
| 27 | const handleEmailSignIn = async (e: React.FormEvent) => { |
| 28 | e.preventDefault(); |
| 29 | setError(""); |
| 30 | setLoading(true); |
| 31 | |
| 32 | const result = await signIn.email({ |
| 33 | email, |
| 34 | password, |
| 35 | }); |
| 36 | |
| 37 | if (result.error) { |
| 38 | setError(result.error.message || "An error occurred"); |
| 39 | setLoading(false); |
| 40 | } else { |
| 41 | router.push("/repos"); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | const handleGithubSignIn = async () => { |
| 46 | setError(""); |
| 47 | setLoading(true); |
| 48 | |
| 49 | await signIn.social({ |
| 50 | provider: "github", |
| 51 | callbackURL: "/repos", |
| 52 | }); |
| 53 | }; |
| 54 | |
| 55 | return ( |
| 56 | <div className="flex min-h-screen items-center justify-center p-4"> |
| 57 | <Card className="w-full max-w-md"> |
| 58 | <CardHeader className="text-center"> |
| 59 | <CardTitle className="text-2xl">Sign In</CardTitle> |
| 60 | <CardDescription> |
| 61 | Sign in with your email or GitHub account. |
| 62 | </CardDescription> |
| 63 | </CardHeader> |
| 64 | <CardContent className="space-y-4"> |
| 65 | <Button |
| 66 | variant="outline" |
| 67 | className="w-full" |
| 68 | onClick={handleGithubSignIn} |
| 69 | disabled={loading} |
| 70 | > |
| 71 | <FaGithub className="mr-2 size-4" /> |
| 72 | Sign in with GitHub |
| 73 | </Button> |
| 74 | |
| 75 | <div className="relative"> |
| 76 | <div className="absolute inset-0 flex items-center"> |
| 77 | <Separator className="w-full" /> |
| 78 | </div> |
| 79 | <div className="relative flex justify-center text-xs uppercase"> |
| 80 | <span className="bg-card px-2 text-muted-foreground"> |
| 81 | Or continue with email |
| 82 | </span> |
| 83 | </div> |
| 84 | </div> |
| 85 | |
| 86 | <form onSubmit={handleEmailSignIn} className="space-y-4"> |
| 87 | <div className="space-y-2"> |
| 88 | <Label htmlFor="email">Email</Label> |
| 89 | <Input |
| 90 | id="email" |
| 91 | type="email" |
| 92 | placeholder="name@example.com" |
| 93 | value={email} |
| 94 | onChange={(e) => setEmail(e.target.value)} |
| 95 | disabled={loading} |
| 96 | /> |
| 97 | </div> |
| 98 | <div className="space-y-2"> |
| 99 | <Label htmlFor="password">Password</Label> |
| 100 | <Input |
| 101 | id="password" |
| 102 | type="password" |
| 103 | placeholder="********" |
| 104 | value={password} |
| 105 | onChange={(e) => setPassword(e.target.value)} |
| 106 | disabled={loading} |
| 107 | /> |
| 108 | </div> |
| 109 | |
| 110 | {error && <p className="text-red-500">{error}</p>} |
| 111 | |
| 112 | <Button type="submit" className="w-full" disabled={loading}> |
| 113 | {loading ? "Loading..." : "Sign In"} |
| 114 | </Button> |
| 115 | </form> |
| 116 | |
| 117 | <p className="text-center text-sm text-muted-foreground"> |
| 118 | Don't have an account? <Link href="/sign-up">Sign Up</Link> |
| 119 | </p> |
| 120 | </CardContent> |
| 121 | </Card> |
| 122 | </div> |
| 123 | ); |
| 124 | } |