| 1 | import { |
| 2 | authMutationKeys, |
| 3 | authQueryKeys, |
| 4 | getAuthErrorPresentation, |
| 5 | isPasswordCompromisedError, |
| 6 | } from "@better-auth-ui/core"; |
| 7 | import { oneTapMutationKeys } |
| 8 | import { |
| 9 | matchMutation, |
| 10 | matchQuery, |
| 11 | useQueryClient, |
| 12 | } from "@tanstack/react-query"; |
| 13 | import type { BetterFetchError |
| 14 | import { useEffect } |
| 15 | import { toast } |
| 16 | import { isSessionNotFreshError } |
| 17 | |
| 18 | export function ErrorToaster() |
| 19 | const queryClient = |
| 20 | |
| 21 | useEffect(() => |
| 22 | const queryCache = |
| 23 | const previousQueryOnError = |
| 24 | |
| 25 | queryCache.config.onError |
| 26 | previousQueryOnError?.(error, |
| 27 | |
| 28 | if (!matchQuery({ |
| 29 | if (getAuthErrorPresentation(query |
| 30 | if (error.name |
| 31 | |
| 32 | const err = |
| 33 | if (isSessionNotFreshError(error |
| 34 | if (err?.error |
| 35 | }; |
| 36 | |
| 37 | const mutationCache = |
| 38 | const previousMutationOnError = |
| 39 | |
| 40 | mutationCache.config.onError |
| 41 | error, |
| 42 | variables, |
| 43 | onMutateResult, |
| 44 | mutation, |
| 45 | context, |
| 46 | ) => { |
| 47 | previousMutationOnError?.( |
| 48 | error, |
| 49 | variables, |
| 50 | onMutateResult, |
| 51 | mutation, |
| 52 | context, |
| 53 | ); |
| 54 | |
| 55 | if (!matchMutation({ |
| 56 | return; |
| 57 | } |
| 58 | if (getAuthErrorPresentation(mutation |
| 59 | if (isSessionNotFreshError(error |
| 60 | // Every form that sets a new password renders this one against the password field, so a toast would just repeat it. |
| 61 | if (isPasswordCompromisedError(error |
| 62 | |
| 63 | const err = |
| 64 | if ( |
| 65 | err.error?.code |
| 66 | !matchMutation({ mutationKey: |
| 67 | ) { |
| 68 | return; |
| 69 | } |
| 70 | toast.error(err |
| 71 | }; |
| 72 | |
| 73 | return () => |
| 74 | queryCache.config.onError |
| 75 | mutationCache.config.onError |
| 76 | }; |
| 77 | }, [queryClient]); |
| 78 | |
| 79 | return null; |
| 80 | } |