| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | type AuthSocialProvider, |
| 5 | type AuthView, |
| 6 | authMutationKeys, |
| 7 | getProviderId, |
| 8 | getProviderName, |
| 9 | type OAuthPopupAuthClient, |
| 10 | } from "@better-auth-ui/core"; |
| 11 | import { |
| 12 | renderProviderIcon, |
| 13 | useAuth, |
| 14 | useFetchOptions, |
| 15 | useSignInOAuthPopup, |
| 16 | useSignInSocial, |
| 17 | } from "@better-auth-ui/react"; |
| 18 | import { useIsMutating } |
| 19 | import type { ComponentProps |
| 20 | |
| 21 | import { Button } |
| 22 | import { Spinner } |
| 23 | import { cn } |
| 24 | import { LastUsedBadge } |
| 25 | |
| 26 | export type ProviderButtonProps |
| 27 | provider: AuthSocialProvider |
| 28 | display?: " |
| 29 | view?: AuthView |
| 30 | } & Omit |
| 31 | |
| 32 | /** Social provider sign-in button. */ |
| 33 | export function ProviderButton({ |
| 34 | provider, |
| 35 | display = "full" |
| 36 | view = "signIn" |
| 37 | variant = "outline" |
| 38 | className, |
| 39 | ...props |
| 40 | }: ProviderButtonProps) |
| 41 | const { |
| 42 | authClient, |
| 43 | baseURL, |
| 44 | localization, |
| 45 | navigate, |
| 46 | redirectTo, |
| 47 | socialSignInMode, |
| 48 | } = useAuth(); |
| 49 | |
| 50 | const callbackURL = |
| 51 | const { fetchOptions, |
| 52 | |
| 53 | const { mutate: |
| 54 | useSignInSocial(authClient, { |
| 55 | const { mutate: |
| 56 | useSignInOAuthPopup(authClient as |
| 57 | onError: resetFetchOptions, |
| 58 | }); |
| 59 | |
| 60 | const providerId = |
| 61 | const providerIcon = |
| 62 | |
| 63 | const signInMutating = |
| 64 | mutationKey: authMutationKeys.signIn |
| 65 | }); |
| 66 | const signUpMutating = |
| 67 | mutationKey: authMutationKeys.signUp |
| 68 | }); |
| 69 | const isPending = |
| 70 | |
| 71 | const handleSignIn = |
| 72 | if (socialSignInMode === |
| 73 | signInPopup( |
| 74 | { |
| 75 | provider: providerId, |
| 76 | callbackURL, |
| 77 | requestSignUp: view === |
| 78 | }, |
| 79 | { onSuccess: () |
| 80 | ); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | signInSocial({ provider: |
| 85 | }; |
| 86 | |
| 87 | return ( |
| 88 | <Button |
| 89 | type="button" |
| 90 | variant={variant} |
| 91 | disabled={isPending} |
| 92 | onClick={handleSignIn} |
| 93 | className={cn(" |
| 94 | {...props} |
| 95 | > |
| 96 | {signInSocialPending || signInPopupPending |
| 97 | |
| 98 | {display === "full |
| 99 | ? localization.auth. |
| 100 | "{{provider}}", |
| 101 | getProviderName(provider), |
| 102 | ) |
| 103 | : display === " |
| 104 | ? getProviderName(provider) |
| 105 | : null} |
| 106 | |
| 107 | {display === "icon |
| 108 | <span className="sr-only |
| 109 | )} |
| 110 | |
| 111 | {view !== "signUp |
| 112 | </Button> |
| 113 | ); |
| 114 | } |