|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button } from "@/components/ui/button"; |
| 4 | +import { |
| 5 | + Card, |
| 6 | + CardContent, |
| 7 | + CardDescription, |
| 8 | + CardHeader, |
| 9 | + CardTitle, |
| 10 | +} from "@/components/ui/card"; |
| 11 | +import { |
| 12 | + Field, |
| 13 | + FieldDescription, |
| 14 | + FieldGroup, |
| 15 | + FieldLabel, |
| 16 | + FieldSeparator, |
| 17 | +} from "@/components/ui/field"; |
| 18 | +import { Input } from "@/components/ui/input"; |
| 19 | +import { createClient } from "@/lib/supabase/client"; |
| 20 | +import Image from "next/image"; |
| 21 | +import type { Provider } from "@supabase/supabase-js"; |
| 22 | +import Link from "next/link"; |
| 23 | +import { useRouter } from "next/navigation"; |
| 24 | +import { useState } from "react"; |
| 25 | + |
| 26 | +const Login = () => { |
| 27 | + const [email, setEmail] = useState(""); |
| 28 | + const [password, setPassword] = useState(""); |
| 29 | + const [error, setError] = useState<string | null>(null); |
| 30 | + const [isLoading, setIsLoading] = useState(false); |
| 31 | + const router = useRouter(); |
| 32 | + |
| 33 | + const supabase = createClient(); |
| 34 | + |
| 35 | + const handleLogin = async (e: React.FormEvent) => { |
| 36 | + e.preventDefault(); |
| 37 | + setIsLoading(true); |
| 38 | + setError(null); |
| 39 | + |
| 40 | + try { |
| 41 | + const { error } = await supabase.auth.signInWithPassword({ |
| 42 | + email, |
| 43 | + password, |
| 44 | + }); |
| 45 | + if (error) throw error; |
| 46 | + router.push("/"); |
| 47 | + } catch (error: unknown) { |
| 48 | + setError(error instanceof Error ? error.message : "An error occurred"); |
| 49 | + } finally { |
| 50 | + setIsLoading(false); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const handleOAuthLogin = async (provider: Provider) => { |
| 55 | + setIsLoading(true); |
| 56 | + setError(null); |
| 57 | + |
| 58 | + try { |
| 59 | + const { error } = await supabase.auth.signInWithOAuth({ |
| 60 | + provider, |
| 61 | + options: { |
| 62 | + redirectTo: `${process.env.SITE_URL}/auth/callback`, |
| 63 | + }, |
| 64 | + }); |
| 65 | + if (error) throw error; |
| 66 | + } catch (error: unknown) { |
| 67 | + setError(error instanceof Error ? error.message : "An error occurred"); |
| 68 | + } finally { |
| 69 | + setIsLoading(false); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="bg-zinc-50 dark:bg-neutral-900/80 flex min-h-svh w-full items-center justify-center p-6 md:p-10"> |
| 75 | + <div className="w-full max-w-sm"> |
| 76 | + <div className="flex flex-col gap-6"> |
| 77 | + <Card> |
| 78 | + <CardHeader className="text-center"> |
| 79 | + <CardTitle className="text-xl">Welcome back</CardTitle> |
| 80 | + <CardDescription> |
| 81 | + Login with your GitHub or Google account |
| 82 | + </CardDescription> |
| 83 | + </CardHeader> |
| 84 | + <CardContent> |
| 85 | + <form onSubmit={handleLogin}> |
| 86 | + <FieldGroup> |
| 87 | + <Field> |
| 88 | + <Button |
| 89 | + onClick={() => handleOAuthLogin("github")} |
| 90 | + variant="outline" |
| 91 | + type="button" |
| 92 | + > |
| 93 | + <Image |
| 94 | + width="24" |
| 95 | + height="24" |
| 96 | + src="https://img.icons8.com/material-outlined/24/github.png" |
| 97 | + alt="github" |
| 98 | + /> |
| 99 | + Login with GitHub |
| 100 | + </Button> |
| 101 | + <Button |
| 102 | + onClick={() => handleOAuthLogin("google")} |
| 103 | + variant="outline" |
| 104 | + type="button" |
| 105 | + > |
| 106 | + <Image |
| 107 | + width="24" |
| 108 | + height="24" |
| 109 | + src="https://img.icons8.com/material-rounded/24/google-logo.png" |
| 110 | + alt="google-logo" |
| 111 | + /> |
| 112 | + Login with Google |
| 113 | + </Button> |
| 114 | + </Field> |
| 115 | + <FieldSeparator className="*:data-[slot=field-separator-content]:bg-card"> |
| 116 | + Or continue with |
| 117 | + </FieldSeparator> |
| 118 | + <Field> |
| 119 | + <FieldLabel htmlFor="email">Email</FieldLabel> |
| 120 | + <Input |
| 121 | + id="email" |
| 122 | + type="email" |
| 123 | + |
| 124 | + required |
| 125 | + value={email} |
| 126 | + onChange={(e) => setEmail(e.target.value)} |
| 127 | + /> |
| 128 | + </Field> |
| 129 | + <Field> |
| 130 | + <div className="flex items-center"> |
| 131 | + <FieldLabel htmlFor="password">Password</FieldLabel> |
| 132 | + <Link |
| 133 | + href="/forgot-password" |
| 134 | + className="ml-auto text-sm underline-offset-4 hover:underline" |
| 135 | + > |
| 136 | + Forgot your password? |
| 137 | + </Link> |
| 138 | + </div> |
| 139 | + <Input |
| 140 | + id="password" |
| 141 | + type="password" |
| 142 | + required |
| 143 | + value={password} |
| 144 | + onChange={(e) => setPassword(e.target.value)} |
| 145 | + /> |
| 146 | + </Field> |
| 147 | + {error && <p className="text-sm text-red-500">{error}</p>} |
| 148 | + <Field> |
| 149 | + <Button |
| 150 | + type="submit" |
| 151 | + className="w-full" |
| 152 | + disabled={isLoading} |
| 153 | + > |
| 154 | + {isLoading ? "Logging in..." : "Login"} |
| 155 | + </Button> |
| 156 | + <FieldDescription className="text-center"> |
| 157 | + Don't have an account?{" "} |
| 158 | + <Link href="/signup">Sign up</Link> |
| 159 | + </FieldDescription> |
| 160 | + </Field> |
| 161 | + </FieldGroup> |
| 162 | + </form> |
| 163 | + </CardContent> |
| 164 | + </Card> |
| 165 | + </div> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + ); |
| 169 | +}; |
| 170 | + |
| 171 | +export default Login; |
0 commit comments