| 1 | "use client"; |
| 2 | |
| 3 | import { |
| 4 | getReauthenticationSignInURL, |
| 5 | isReauthenticationSignInURL, |
| 6 | } from "@better-auth-ui/core"; |
| 7 | import { useAuth, useSignOut |
| 8 | import { useSyncExternalStore } |
| 9 | import { Alert, AlertDescription |
| 10 | import { Button } |
| 11 | import { Spinner } |
| 12 | import { cn } |
| 13 | |
| 14 | const subscribeToLocation = () |
| 15 | |
| 16 | function useIsReauthenticationSignIn() { |
| 17 | return useSyncExternalStore( |
| 18 | subscribeToLocation, |
| 19 | () => isReauthenticationSignInURL( |
| 20 | () => false, |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | export type ReauthenticationActionProps |
| 25 | className?: string |
| 26 | showTitle?: boolean |
| 27 | }; |
| 28 | |
| 29 | export function ReauthenticationAction({ |
| 30 | className, |
| 31 | showTitle = true, |
| 32 | }: ReauthenticationActionProps) |
| 33 | const auth = |
| 34 | const signOut = |
| 35 | |
| 36 | const handleReauthentication = |
| 37 | const signInURL = |
| 38 | new URL(window |
| 39 | `${auth.basePaths. |
| 40 | ); |
| 41 | signOut.mutate(undefined, |
| 42 | onSuccess: () => |
| 43 | }); |
| 44 | }; |
| 45 | |
| 46 | return ( |
| 47 | <div className={cn |
| 48 | <div className="flex flex-col gap-1 |
| 49 | {showTitle ? ( |
| 50 | <h3 className="text-sm font-medium |
| 51 | {auth.localization.settings |
| 52 | </h3> |
| 53 | ) : null} |
| 54 | <p className="text-sm text-muted-foreground |
| 55 | {auth.localization.settings |
| 56 | </p> |
| 57 | </div> |
| 58 | <Button |
| 59 | disabled={signOut.isPending |
| 60 | onClick={handleReauthentication} |
| 61 | size="sm" |
| 62 | > |
| 63 | {signOut.isPending ? |
| 64 | {auth.localization.settings |
| 65 | </Button> |
| 66 | </div> |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | export function ReauthenticationNotice() |
| 71 | const auth = |
| 72 | const isReauthenticationSignIn = |
| 73 | if (!isReauthenticationSignIn) |
| 74 | |
| 75 | return ( |
| 76 | <Alert className=" |
| 77 | <AlertTitle> |
| 78 | {auth.localization.settings |
| 79 | </AlertTitle> |
| 80 | <AlertDescription> |
| 81 | {auth.localization.settings |
| 82 | </AlertDescription> |
| 83 | </Alert> |
| 84 | ); |
| 85 | } |