Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

chore: Refactor login page components and improve error handling

Kiran K8fcf9a7a01ae
diff --git a/apps/web/app/app.dub.co/(auth)/login/page.tsx b/apps/web/app/app.dub.co/(auth)/login/page.tsx
index b9980ea327..a22fdd0527 100644
--- a/apps/web/app/app.dub.co/(auth)/login/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/login/page.tsx
@@ -22,21 +22,23 @@ export default function LoginPage() {
Start creating short links with superpowers
</p>
</div>
- <div className="flex flex-col space-y-3 bg-gray-50 px-4 py-8 sm:px-16">
- <Suspense
- fallback={
- <>
- <Button disabled={true} variant="secondary" />
- <Button disabled={true} variant="secondary" />
- <Button disabled={true} variant="secondary" />
- <div className="mx-auto h-5 w-3/4 rounded-lg bg-gray-100" />
- </>
- }
- >
- <SignInWithEmailPassword />
- <div className="border-t border-gray-300" />
- <LoginForm />
- </Suspense>
+ <div className="flex flex-col bg-gray-50 px-4 py-8 sm:px-16">
+ <SignInWithEmailPassword />
+ <div className="my-8 border-t border-gray-300 bg-red-300" />
+ <div className="space-y-3">
+ <Suspense
+ fallback={
+ <>
+ <Button disabled={true} variant="secondary" />
+ <Button disabled={true} variant="secondary" />
+ <Button disabled={true} variant="secondary" />
+ <div className="mx-auto h-5 w-3/4 rounded-lg bg-gray-100" />
+ </>
+ }
+ >
+ <LoginForm />
+ </Suspense>
+ </div>
</div>
</div>
);
diff --git a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
index 6b2ef21a0e..6d82cd086c 100644
--- a/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
+++ b/apps/web/app/app.dub.co/(auth)/login/signin-email-password.tsx
@@ -2,7 +2,7 @@
import z from "@/lib/zod";
import { signInSchema } from "@/lib/zod/schemas/auth";
-import { Button } from "@dub/ui";
+import { Button, Input } from "@dub/ui";
import { signIn } from "next-auth/react";
import { useRouter, useSearchParams } from "next/navigation";
import { useState } from "react";
@@ -19,21 +19,17 @@ const errorCodes = {
export const SignInWithEmailPassword = () => {
const router = useRouter();
const searchParams = useSearchParams();
- const [submitting, setSubmitting] = useState(false);
+ const [error, setError] = useState<string | null>(null);
const {
register,
resetField,
handleSubmit,
- formState: { isLoading },
- } = useForm<z.infer<typeof signInSchema>>({
- defaultValues: { email: "", password: "" },
- });
+ formState: { isSubmitting, disabled },
+ } = useForm<z.infer<typeof signInSchema>>();
const onSubmit = handleSubmit(async (data) => {
try {
- setSubmitting(true);
-
const response = await signIn("credentials", {
...data,
redirect: false,
@@ -55,37 +51,49 @@ export const SignInWithEmailPassword = () => {
return;
}
} catch (error) {
- setSubmitting(false);
- toast.error(error.message);
+ setError(error.message);
}
});
return (
- <form onSubmit={onSubmit}>
- <div className="flex flex-col space-y-4">
- <input
- type="email"
- placeholder="[email protected]"
- autoComplete="email"
- required
- {...register("email")}
- className="mt-1 block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black sm:text-sm"
- />
- <input
- type="password"
- placeholder="********"
- autoComplete="password"
- required
- {...register("password")}
- className="mt-1 block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black sm:text-sm"
- />
- <Button
- text="Sign in"
- type="submit"
- loading={isLoading}
- disabled={submitting}
- />
- </div>
- </form>
+ <>
+ {error && (
+ <div className="mb-4 rounded-md bg-red-100 p-3 text-red-900 dark:bg-red-900 dark:text-red-200">
+ <div className="relative flex md:flex-row">
+ <div className="flex flex-grow flex-col sm:flex-row">
+ <div className="ltr:ml-3 rtl:mr-3">
+ <h3 className="text-sm font-medium">
+ {errorCodes[error] || error}
+ </h3>
+ </div>
+ </div>
+ </div>
+ </div>
+ )}
+
+ <form onSubmit={onSubmit}>
+ <div className="flex flex-col space-y-4">
+ <Input
+ type="email"
+ placeholder="Email address"
+ autoComplete="email"
+ required
+ {...register("email")}
+ />
+ <Input
+ type="password"
+ placeholder="Password"
+ required
+ {...register("password")}
+ />
+ <Button
+ text="Sign in"
+ type="submit"
+ loading={isSubmitting}
+ disabled={disabled}
+ />
+ </div>
+ </form>
+ </>
);
};