|
| 1 | +import { useState } from "react"; |
| 2 | +import type { FormEvent } from "react"; |
| 3 | + |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { |
| 6 | + Card, |
| 7 | + CardContent, |
| 8 | + CardDescription, |
| 9 | + CardHeader, |
| 10 | + CardTitle, |
| 11 | +} from "@/components/ui/card"; |
| 12 | +import { |
| 13 | + Field, |
| 14 | + FieldDescription, |
| 15 | + FieldError, |
| 16 | + FieldGroup, |
| 17 | + FieldLabel, |
| 18 | +} from "@/components/ui/field"; |
| 19 | +import { Input } from "@/components/ui/input"; |
| 20 | +import { cn } from "@/lib/utils"; |
| 21 | + |
| 22 | +const LOGIN_MUTATION = ` |
| 23 | + mutation DashboardLogin($input: LoginInput!) { |
| 24 | + login(input: $input) { |
| 25 | + __typename |
| 26 | + ... on LoginSuccess { |
| 27 | + user { |
| 28 | + id |
| 29 | + } |
| 30 | + } |
| 31 | + ... on LoginErrors { |
| 32 | + errors { |
| 33 | + email |
| 34 | + password |
| 35 | + } |
| 36 | + } |
| 37 | + ... on WrongEmailOrPassword { |
| 38 | + message |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +`; |
| 43 | + |
| 44 | +type LoginErrors = { |
| 45 | + email: string[]; |
| 46 | + password: string[]; |
| 47 | + form: string[]; |
| 48 | +}; |
| 49 | + |
| 50 | +type LoginResponse = { |
| 51 | + data?: { |
| 52 | + login?: |
| 53 | + | { __typename: "LoginSuccess" } |
| 54 | + | { |
| 55 | + __typename: "LoginErrors"; |
| 56 | + errors: { email: string[]; password: string[] }; |
| 57 | + } |
| 58 | + | { __typename: "WrongEmailOrPassword"; message: string }; |
| 59 | + }; |
| 60 | + errors?: Array<{ message: string }>; |
| 61 | +}; |
| 62 | + |
| 63 | +export function LoginForm({ |
| 64 | + className, |
| 65 | + nextUrl, |
| 66 | + ...props |
| 67 | +}: React.ComponentProps<"div"> & { nextUrl: string }) { |
| 68 | + const [errors, setErrors] = useState<LoginErrors>({ |
| 69 | + email: [], |
| 70 | + password: [], |
| 71 | + form: [], |
| 72 | + }); |
| 73 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 74 | + |
| 75 | + async function logIn(event: FormEvent<HTMLFormElement>) { |
| 76 | + event.preventDefault(); |
| 77 | + setErrors({ email: [], password: [], form: [] }); |
| 78 | + setIsSubmitting(true); |
| 79 | + |
| 80 | + const formData = new FormData(event.currentTarget); |
| 81 | + |
| 82 | + try { |
| 83 | + const response = await fetch("/graphql", { |
| 84 | + method: "POST", |
| 85 | + headers: { "Content-Type": "application/json" }, |
| 86 | + credentials: "same-origin", |
| 87 | + body: JSON.stringify({ |
| 88 | + query: LOGIN_MUTATION, |
| 89 | + variables: { |
| 90 | + input: { |
| 91 | + email: formData.get("email"), |
| 92 | + password: formData.get("password"), |
| 93 | + }, |
| 94 | + }, |
| 95 | + }), |
| 96 | + }); |
| 97 | + |
| 98 | + if (!response.ok) { |
| 99 | + throw new Error(`Login request failed with status ${response.status}`); |
| 100 | + } |
| 101 | + |
| 102 | + const payload = (await response.json()) as LoginResponse; |
| 103 | + |
| 104 | + if (payload.errors?.length) { |
| 105 | + setErrors({ |
| 106 | + email: [], |
| 107 | + password: [], |
| 108 | + form: payload.errors.map((error) => error.message), |
| 109 | + }); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + const result = payload.data?.login; |
| 114 | + |
| 115 | + if (result?.__typename === "LoginSuccess") { |
| 116 | + window.location.assign(nextUrl); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + if (result?.__typename === "LoginErrors") { |
| 121 | + setErrors({ ...result.errors, form: [] }); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (result?.__typename === "WrongEmailOrPassword") { |
| 126 | + setErrors({ |
| 127 | + email: [], |
| 128 | + password: ["The email or password is incorrect."], |
| 129 | + form: [], |
| 130 | + }); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + throw new Error("Login response did not contain a result"); |
| 135 | + } catch { |
| 136 | + setErrors({ |
| 137 | + email: [], |
| 138 | + password: [], |
| 139 | + form: ["We couldn't log you in. Please try again."], |
| 140 | + }); |
| 141 | + } finally { |
| 142 | + setIsSubmitting(false); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return ( |
| 147 | + <div className={cn("flex flex-col gap-6", className)} {...props}> |
| 148 | + <Card> |
| 149 | + <CardHeader className="text-center"> |
| 150 | + <CardTitle className="text-xl">Welcome back</CardTitle> |
| 151 | + <CardDescription> |
| 152 | + Sign in with your email and password. |
| 153 | + </CardDescription> |
| 154 | + </CardHeader> |
| 155 | + <CardContent> |
| 156 | + <form onSubmit={logIn}> |
| 157 | + <FieldGroup> |
| 158 | + <Field data-invalid={errors.email.length > 0}> |
| 159 | + <FieldLabel htmlFor="email">Email</FieldLabel> |
| 160 | + <Input |
| 161 | + aria-describedby={ |
| 162 | + errors.email.length ? "email-error" : undefined |
| 163 | + } |
| 164 | + aria-invalid={errors.email.length > 0} |
| 165 | + autoComplete="email" |
| 166 | + disabled={isSubmitting} |
| 167 | + id="email" |
| 168 | + name="email" |
| 169 | + type="email" |
| 170 | + placeholder="m@example.com" |
| 171 | + required |
| 172 | + /> |
| 173 | + <FieldError |
| 174 | + errors={errors.email.map((message) => ({ message }))} |
| 175 | + id="email-error" |
| 176 | + /> |
| 177 | + </Field> |
| 178 | + <Field data-invalid={errors.password.length > 0}> |
| 179 | + <div className="flex items-center"> |
| 180 | + <FieldLabel htmlFor="password">Password</FieldLabel> |
| 181 | + <a |
| 182 | + href="/reset-password" |
| 183 | + className="ml-auto text-sm underline-offset-4 hover:underline" |
| 184 | + > |
| 185 | + Forgot your password? |
| 186 | + </a> |
| 187 | + </div> |
| 188 | + <Input |
| 189 | + aria-describedby={ |
| 190 | + errors.password.length ? "password-error" : undefined |
| 191 | + } |
| 192 | + aria-invalid={errors.password.length > 0} |
| 193 | + autoComplete="current-password" |
| 194 | + disabled={isSubmitting} |
| 195 | + id="password" |
| 196 | + name="password" |
| 197 | + type="password" |
| 198 | + required |
| 199 | + /> |
| 200 | + <FieldError |
| 201 | + errors={errors.password.map((message) => ({ message }))} |
| 202 | + id="password-error" |
| 203 | + /> |
| 204 | + </Field> |
| 205 | + <Field> |
| 206 | + <Button disabled={isSubmitting} type="submit"> |
| 207 | + {isSubmitting ? "Logging in…" : "Login"} |
| 208 | + </Button> |
| 209 | + <FieldError |
| 210 | + errors={errors.form.map((message) => ({ message }))} |
| 211 | + /> |
| 212 | + <FieldDescription className="text-center"> |
| 213 | + Don't have an account? <a href="/signup">Sign up</a> |
| 214 | + </FieldDescription> |
| 215 | + </Field> |
| 216 | + </FieldGroup> |
| 217 | + </form> |
| 218 | + </CardContent> |
| 219 | + </Card> |
| 220 | + <FieldDescription className="px-6 text-center"> |
| 221 | + By clicking continue, you agree to our{" "} |
| 222 | + <a href="/terms-of-service">Terms of Service</a> and{" "} |
| 223 | + <a href="/privacy-policy">Privacy Policy</a>. |
| 224 | + </FieldDescription> |
| 225 | + </div> |
| 226 | + ); |
| 227 | +} |
0 commit comments