|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import Link from "next/link"; |
| 6 | +import { createClient } from "@/lib/supabase/client"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { Input } from "@/components/ui/input"; |
| 9 | +import { Label } from "@/components/ui/label"; |
| 10 | +import { |
| 11 | + Card, |
| 12 | + CardContent, |
| 13 | + CardDescription, |
| 14 | + CardHeader, |
| 15 | + CardTitle, |
| 16 | +} from "@/components/ui/card"; |
| 17 | +import { OAuthButtons } from "@/components/auth/oauth-buttons"; |
| 18 | + |
| 19 | +export default function SignInPage() { |
| 20 | + const router = useRouter(); |
| 21 | + const [email, setEmail] = useState(""); |
| 22 | + const [password, setPassword] = useState(""); |
| 23 | + const [error, setError] = useState<string | null>(null); |
| 24 | + const [loading, setLoading] = useState(false); |
| 25 | + |
| 26 | + async function handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 27 | + e.preventDefault(); |
| 28 | + setError(null); |
| 29 | + setLoading(true); |
| 30 | + |
| 31 | + const supabase = createClient(); |
| 32 | + const { error: signInError } = await supabase.auth.signInWithPassword({ |
| 33 | + email, |
| 34 | + password, |
| 35 | + }); |
| 36 | + |
| 37 | + if (signInError) { |
| 38 | + setError(signInError.message); |
| 39 | + setLoading(false); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Fetch the user's personal workspace to redirect to |
| 44 | + const { |
| 45 | + data: { user }, |
| 46 | + } = await supabase.auth.getUser(); |
| 47 | + if (user) { |
| 48 | + const { data: membership } = await supabase |
| 49 | + .from("members") |
| 50 | + .select("workspace_id, workspaces(slug)") |
| 51 | + .eq("user_id", user.id) |
| 52 | + .limit(1) |
| 53 | + .maybeSingle(); |
| 54 | + |
| 55 | + if (membership?.workspaces) { |
| 56 | + const ws = membership.workspaces as unknown as { slug: string }; |
| 57 | + router.push(`/${ws.slug}`); |
| 58 | + return; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Fallback: redirect to root which will handle routing |
| 63 | + router.push("/"); |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <Card> |
| 68 | + <CardHeader> |
| 69 | + <CardTitle className="text-2xl font-bold">Sign in to Memo</CardTitle> |
| 70 | + <CardDescription> |
| 71 | + Enter your email and password to continue. |
| 72 | + </CardDescription> |
| 73 | + </CardHeader> |
| 74 | + <CardContent> |
| 75 | + <form onSubmit={handleSubmit} className="flex flex-col gap-3"> |
| 76 | + <div className="flex flex-col gap-1.5"> |
| 77 | + <Label htmlFor="email">Email</Label> |
| 78 | + <Input |
| 79 | + id="email" |
| 80 | + type="email" |
| 81 | + placeholder="you@example.com" |
| 82 | + value={email} |
| 83 | + onChange={(e) => setEmail(e.target.value)} |
| 84 | + required |
| 85 | + autoComplete="email" |
| 86 | + autoFocus |
| 87 | + /> |
| 88 | + </div> |
| 89 | + <div className="flex flex-col gap-1.5"> |
| 90 | + <Label htmlFor="password">Password</Label> |
| 91 | + <Input |
| 92 | + id="password" |
| 93 | + type="password" |
| 94 | + placeholder="••••••••" |
| 95 | + value={password} |
| 96 | + onChange={(e) => setPassword(e.target.value)} |
| 97 | + required |
| 98 | + autoComplete="current-password" |
| 99 | + minLength={6} |
| 100 | + /> |
| 101 | + </div> |
| 102 | + {error && <p className="text-xs text-destructive">{error}</p>} |
| 103 | + <Button type="submit" disabled={loading} className="mt-1"> |
| 104 | + {loading ? "Signing in…" : "Sign in"} |
| 105 | + </Button> |
| 106 | + </form> |
| 107 | + <div className="relative my-4"> |
| 108 | + <div className="absolute inset-0 flex items-center"> |
| 109 | + <span className="w-full border-t border-white/[0.06]" /> |
| 110 | + </div> |
| 111 | + <div className="relative flex justify-center text-xs"> |
| 112 | + <span className="bg-card px-2 text-muted-foreground">or</span> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + <OAuthButtons /> |
| 116 | + <p className="mt-4 text-center text-xs text-muted-foreground"> |
| 117 | + Don't have an account?{" "} |
| 118 | + <Link href="/sign-up" className="text-accent underline-offset-4 hover:underline"> |
| 119 | + Sign up |
| 120 | + </Link> |
| 121 | + </p> |
| 122 | + </CardContent> |
| 123 | + </Card> |
| 124 | + ); |
| 125 | +} |
0 commit comments