page.tsx138 lines, 4.0 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, signUp } 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 | import { set } from "zod"; |
| 20 | |
| 21 | export default function SignUpPage() { |
| 22 | const router = useRouter(); |
| 23 | const [name, setName] = useState(""); |
| 24 | const [email, setEmail] = useState(""); |
| 25 | const [password, setPassword] = useState(""); |
| 26 | const [error, setError] = useState(""); |
| 27 | const [loading, setLoading] = useState(false); |
| 28 | |
| 29 | const handleEmailSignIn = async (e: React.FormEvent) => { |
| 30 | e.preventDefault(); |
| 31 | setError(""); |
| 32 | setLoading(true); |
| 33 | |
| 34 | const result = await signUp.email({ |
| 35 | name, |
| 36 | email, |
| 37 | password, |
| 38 | }); |
| 39 | |
| 40 | if (result.error) { |
| 41 | setError(result.error.message || "An error occurred"); |
| 42 | setLoading(false); |
| 43 | } else { |
| 44 | router.push("/repos"); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | const handleGithubSignIn = async () => { |
| 49 | setError(""); |
| 50 | setLoading(true); |
| 51 | |
| 52 | await signIn.social({ |
| 53 | provider: "github", |
| 54 | callbackURL: "/repos", |
| 55 | }); |
| 56 | }; |
| 57 | |
| 58 | return ( |
| 59 | <div className="flex min-h-screen items-center justify-center p-4"> |
| 60 | <Card className="w-full max-w-md"> |
| 61 | <CardHeader className="text-center"> |
| 62 | <CardTitle className="text-2xl">Sign Up</CardTitle> |
| 63 | <CardDescription> |
| 64 | Sign up with your email or GitHub account. |
| 65 | </CardDescription> |
| 66 | </CardHeader> |
| 67 | <CardContent className="space-y-4"> |
| 68 | <Button |
| 69 | variant="outline" |
| 70 | className="w-full" |
| 71 | onClick={handleGithubSignIn} |
| 72 | disabled={loading} |
| 73 | > |
| 74 | <FaGithub className="mr-2 size-4" /> |
| 75 | Sign up with GitHub |
| 76 | </Button> |
| 77 | |
| 78 | <div className="relative"> |
| 79 | <div className="absolute inset-0 flex items-center"> |
| 80 | <Separator className="w-full" /> |
| 81 | </div> |
| 82 | <div className="relative flex justify-center text-xs uppercase"> |
| 83 | <span className="bg-card px-2 text-muted-foreground"> |
| 84 | Or continue with email |
| 85 | </span> |
| 86 | </div> |
| 87 | </div> |
| 88 | |
| 89 | <form onSubmit={handleEmailSignIn} className="space-y-4"> |
| 90 | <div className="space-y-2"> |
| 91 | <Label htmlFor="name">Name</Label> |
| 92 | <Input |
| 93 | id="name" |
| 94 | type="text" |
| 95 | placeholder="John Doe" |
| 96 | value={name} |
| 97 | onChange={(e) => setName(e.target.value)} |
| 98 | disabled={loading} |
| 99 | /> |
| 100 | </div> |
| 101 | <div className="space-y-2"> |
| 102 | <Label htmlFor="email">Email</Label> |
| 103 | <Input |
| 104 | id="email" |
| 105 | type="email" |
| 106 | placeholder="name@example.com" |
| 107 | value={email} |
| 108 | onChange={(e) => setEmail(e.target.value)} |
| 109 | disabled={loading} |
| 110 | /> |
| 111 | </div> |
| 112 | <div className="space-y-2"> |
| 113 | <Label htmlFor="password">Password</Label> |
| 114 | <Input |
| 115 | id="password" |
| 116 | type="password" |
| 117 | placeholder="********" |
| 118 | value={password} |
| 119 | onChange={(e) => setPassword(e.target.value)} |
| 120 | disabled={loading} |
| 121 | /> |
| 122 | </div> |
| 123 | |
| 124 | {error && <p className="text-red-500">{error}</p>} |
| 125 | |
| 126 | <Button type="submit" className="w-full" disabled={loading}> |
| 127 | {loading ? "Loading..." : "Sign Up"} |
| 128 | </Button> |
| 129 | </form> |
| 130 | |
| 131 | <p className="text-center text-sm text-muted-foreground"> |
| 132 | Already have an account? <Link href="/sign-in">Sign In</Link> |
| 133 | </p> |
| 134 | </CardContent> |
| 135 | </Card> |
| 136 | </div> |
| 137 | ); |
| 138 | } |