-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from emiliosheinz/fix/auth
fix: call getProviders on client side
- Loading branch information
Showing
6 changed files
with
79 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"use client"; | ||
|
||
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"; | ||
|
||
type GetProvidersState = "idle" | "loading" | "success" | "error"; | ||
|
||
type AuthenticationProvidersProps = { | ||
callbackUrl?: string; | ||
}; | ||
|
||
export function AuthenticationProviders({ | ||
callbackUrl, | ||
}: AuthenticationProvidersProps) { | ||
const [providers, setProviders] = | ||
useState<Awaited<ReturnType<typeof getProviders>>>(); | ||
const [getProvidersState, setGetProvidersState] = | ||
useState<GetProvidersState>("idle"); | ||
|
||
useEffect(() => { | ||
setGetProvidersState("loading"); | ||
getProviders() | ||
.then((providers) => { | ||
setProviders(providers); | ||
setGetProvidersState("success"); | ||
}) | ||
.catch(() => { | ||
setGetProvidersState("error"); | ||
}); | ||
}, []); | ||
|
||
const [emailProvider, otherProviders] = useMemo(() => { | ||
if (!providers) return [null, null]; | ||
const email = providers.email; | ||
const other = Object.values(providers).filter( | ||
(provider) => provider.id !== "email", | ||
); | ||
return [email, other]; | ||
}, [providers]); | ||
|
||
if (["loading", "idle"].includes(getProvidersState)) { | ||
return <Loader2 className="mt-10 size-8 animate-spin" />; | ||
} | ||
|
||
if (getProvidersState === "error") { | ||
return ( | ||
<Alert variant="destructive"> | ||
<FiAlertTriangle className="h-4 w-4" /> | ||
<AlertTitle>Erro ao carregar provedores de login</AlertTitle> | ||
<AlertDescription> | ||
<span>Por favor, entre em contato com o nosso suporte em </span> | ||
<a href="mailto:[email protected]">[email protected]</a> | ||
</AlertDescription> | ||
</Alert> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="mt-5 w-full"> | ||
{otherProviders?.map((provider) => ( | ||
<SignInProviderButton | ||
key={provider.id} | ||
provider={provider} | ||
callbackUrl={callbackUrl ?? "/"} | ||
/> | ||
))} | ||
{!!emailProvider && <EmailProviderForm />} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters