Skip to content

Commit

Permalink
feat: improve signin loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliosheinz committed Jun 22, 2024
1 parent 5ef357a commit f7f0ce5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
12 changes: 10 additions & 2 deletions src/app/signin/_components/AuthenticationProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { getProviders } from "next-auth/react";
import { useEffect, useMemo, useState } from "react";
import { SignInProviderButton } from "./SignInProviderButton";
import { EmailProviderForm } from "./EmailProviderForm";
import { Loader2 } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert";
import { FiAlertTriangle } from "react-icons/fi";
import { Skeleton } from "~/components/ui/skeleton";
import { OrSeparator } from "./OrSeparator";

type GetProvidersState = "idle" | "loading" | "success" | "error";

Expand Down Expand Up @@ -53,7 +54,14 @@ export function AuthenticationProviders({
}, [providers]);

if (["loading", "idle"].includes(getProvidersState)) {
return <Loader2 className="mt-10 size-8 animate-spin" />;
return (
<div className="mt-5 flex w-full flex-col gap-5">
<Skeleton className="h-10 w-full" />
<OrSeparator />
<Skeleton className="h-10 w-full" />
<Skeleton className="h-10 w-full" />
</div>
);
}

if (getProvidersState === "error") {
Expand Down
8 changes: 2 additions & 6 deletions src/app/signin/_components/EmailProviderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FormMessage,
} from "~/components/ui/form";
import { Input } from "~/components/ui/input";
import { OrSeparator } from "./OrSeparator";

const formSchema = z.object({
email: z.string().email("Por favor, insira um e-mail válido"),
Expand All @@ -39,12 +40,7 @@ export function EmailProviderForm() {
className="mt-5 flex flex-col gap-5"
onSubmit={form.handleSubmit(onSubmit)}
>
<div className="relative my-5">
<span className="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 transform bg-white pb-1 text-lg tracking-widest text-neutral-500">
ou
</span>
<hr />
</div>
<OrSeparator />
<FormField
name="email"
control={form.control}
Expand Down
10 changes: 10 additions & 0 deletions src/app/signin/_components/OrSeparator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function OrSeparator() {
return (
<div className="relative my-5">
<span className="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 transform bg-white pb-1 text-lg tracking-widest text-neutral-500">
ou
</span>
<hr />
</div>
);
}
9 changes: 5 additions & 4 deletions src/app/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"use client";
import { redirect } from "next/navigation";
import { getServerAuthSession } from "~/server/auth";
import Image from "next/image";
import { Suspense } from "react";
import { Loader2 } from "lucide-react";
import { Alert, AlertDescription, AlertTitle } from "~/components/ui/alert";
import { FiAlertTriangle } from "react-icons/fi";
import { AuthenticationProviders } from "./_components/AuthenticationProviders";
import { useSession } from "next-auth/react";

type SignInPageProps = {
searchParams: Record<string, string>;
};

export default async function SignInPage({ searchParams }: SignInPageProps) {
const session = await getServerAuthSession();
export default function SignInPage({ searchParams }: SignInPageProps) {
const { data: session } = useSession();

if (session) {
redirect(searchParams.callbackUrl ?? "/");
Expand Down Expand Up @@ -41,7 +42,7 @@ export default async function SignInPage({ searchParams }: SignInPageProps) {
</AlertDescription>
</Alert>
)}
<AuthenticationProviders />
<AuthenticationProviders callbackUrl={searchParams.callbackUrl} />
</Suspense>
);
}

0 comments on commit f7f0ce5

Please sign in to comment.