@better-auth-ui/react 1.6.3 → 1.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/auth/auth-provider.d.ts +23 -8
- package/dist/components/auth/auth-provider.js +32 -17
- package/dist/components/{email → auth/email}/email-changed.js +2 -2
- package/dist/components/{email → auth/email}/email-localization.js +1 -1
- package/dist/components/{email → auth/email}/email-styles.js +1 -1
- package/dist/components/{email → auth/email}/email-verification.js +2 -2
- package/dist/components/{email → auth/email}/magic-link.js +2 -2
- package/dist/components/{email → auth/email}/new-device.js +2 -2
- package/dist/components/{email → auth/email}/otp-email.js +2 -2
- package/dist/components/{email → auth/email}/password-changed.js +2 -2
- package/dist/components/{email → auth/email}/reset-password.js +2 -2
- package/dist/email.d.ts +1 -0
- package/dist/email.js +11 -0
- package/dist/hooks/auth/use-authenticate.d.ts +5 -28
- package/dist/hooks/auth/use-authenticate.js +12 -12
- package/dist/hooks/auth/use-user.d.ts +19 -314
- package/dist/hooks/auth/use-user.js +5 -5
- package/dist/hooks/use-auth-mutation.d.ts +12 -7
- package/dist/hooks/use-auth-plugin.d.ts +31 -0
- package/dist/hooks/use-auth-plugin.js +10 -0
- package/dist/hooks/use-auth-query.d.ts +3 -3
- package/dist/index.d.ts +33 -32
- package/dist/index.js +74 -83
- package/dist/lib/auth-client.d.ts +23 -3687
- package/dist/lib/auth-plugin.d.ts +59 -0
- package/dist/lib/auth-server.d.ts +16 -0
- package/dist/mutations/auth/request-password-reset-mutation.d.ts +26 -0
- package/dist/mutations/auth/request-password-reset-mutation.js +24 -0
- package/dist/mutations/auth/reset-password-mutation.d.ts +25 -0
- package/dist/mutations/auth/reset-password-mutation.js +24 -0
- package/dist/mutations/auth/send-verification-email-mutation.d.ts +24 -0
- package/dist/mutations/auth/send-verification-email-mutation.js +24 -0
- package/dist/mutations/auth/sign-in-email-mutation.d.ts +71 -0
- package/dist/mutations/auth/sign-in-email-mutation.js +29 -0
- package/dist/mutations/auth/sign-in-social-mutation.d.ts +77 -0
- package/dist/mutations/auth/sign-in-social-mutation.js +24 -0
- package/dist/mutations/auth/sign-out-mutation.d.ts +25 -0
- package/dist/mutations/auth/sign-out-mutation.js +28 -0
- package/dist/mutations/auth/sign-up-email-mutation.d.ts +105 -0
- package/dist/mutations/auth/sign-up-email-mutation.js +29 -0
- package/dist/mutations/auth-mutation-options.d.ts +37 -20
- package/dist/mutations/auth-mutation-options.js +2 -2
- package/dist/mutations/magic-link/sign-in-magic-link-mutation.d.ts +24 -0
- package/dist/mutations/magic-link/sign-in-magic-link-mutation.js +24 -0
- package/dist/mutations/multi-session/revoke-multi-session-mutation.d.ts +25 -0
- package/dist/mutations/multi-session/revoke-multi-session-mutation.js +29 -0
- package/dist/mutations/multi-session/set-active-session-mutation.d.ts +62 -0
- package/dist/mutations/multi-session/set-active-session-mutation.js +31 -0
- package/dist/mutations/passkey/add-passkey-mutation.d.ts +71 -0
- package/dist/mutations/passkey/add-passkey-mutation.js +29 -0
- package/dist/mutations/passkey/delete-passkey-mutation.d.ts +25 -0
- package/dist/mutations/passkey/delete-passkey-mutation.js +29 -0
- package/dist/mutations/passkey/sign-in-passkey-mutation.d.ts +105 -0
- package/dist/mutations/passkey/sign-in-passkey-mutation.js +29 -0
- package/dist/mutations/settings/change-email-mutation.d.ts +25 -0
- package/dist/mutations/settings/change-email-mutation.js +29 -0
- package/dist/mutations/settings/change-password-mutation.d.ts +78 -0
- package/dist/mutations/settings/change-password-mutation.js +24 -0
- package/dist/mutations/settings/delete-user-mutation.d.ts +26 -0
- package/dist/mutations/settings/delete-user-mutation.js +24 -0
- package/dist/mutations/settings/link-social-mutation.d.ts +26 -0
- package/dist/mutations/settings/link-social-mutation.js +24 -0
- package/dist/mutations/settings/revoke-session-mutation.d.ts +25 -0
- package/dist/mutations/settings/revoke-session-mutation.js +29 -0
- package/dist/mutations/settings/unlink-account-mutation.d.ts +25 -0
- package/dist/mutations/settings/unlink-account-mutation.js +29 -0
- package/dist/mutations/settings/update-user-mutation.d.ts +25 -0
- package/dist/mutations/settings/update-user-mutation.js +35 -0
- package/dist/mutations/username/is-username-available-mutation.d.ts +27 -0
- package/dist/mutations/username/is-username-available-mutation.js +24 -0
- package/dist/mutations/username/sign-in-username-mutation.d.ts +75 -0
- package/dist/mutations/username/sign-in-username-mutation.js +29 -0
- package/dist/queries/auth/session-query.d.ts +71 -0
- package/dist/queries/auth/session-query.js +30 -0
- package/dist/queries/auth-query-options.d.ts +26 -15
- package/dist/queries/multi-session/list-device-sessions-query.d.ts +76 -0
- package/dist/queries/multi-session/list-device-sessions-query.js +31 -0
- package/dist/queries/passkey/list-passkeys-query.d.ts +74 -0
- package/dist/queries/passkey/list-passkeys-query.js +31 -0
- package/dist/queries/settings/account-info-query.d.ts +85 -0
- package/dist/queries/settings/account-info-query.js +31 -0
- package/dist/queries/settings/list-accounts-query.d.ts +76 -0
- package/dist/queries/settings/list-accounts-query.js +31 -0
- package/dist/queries/settings/list-sessions-query.d.ts +74 -0
- package/dist/queries/settings/list-sessions-query.js +31 -0
- package/dist/server/queries/auth/session-query.d.ts +57 -0
- package/dist/server/queries/auth/session-query.js +13 -0
- package/dist/server/queries/multi-session/list-device-sessions-query.d.ts +60 -0
- package/dist/server/queries/multi-session/list-device-sessions-query.js +12 -0
- package/dist/server/queries/passkey/list-passkeys-query.d.ts +60 -0
- package/dist/server/queries/passkey/list-passkeys-query.js +12 -0
- package/dist/server/queries/settings/account-info-query.d.ts +68 -0
- package/dist/server/queries/settings/account-info-query.js +12 -0
- package/dist/server/queries/settings/list-accounts-query.d.ts +60 -0
- package/dist/server/queries/settings/list-accounts-query.js +12 -0
- package/dist/server/queries/settings/list-sessions-query.d.ts +60 -0
- package/dist/server/queries/settings/list-sessions-query.js +12 -0
- package/dist/server.d.ts +7 -0
- package/dist/server.js +7 -0
- package/package.json +24 -22
- package/src/components/auth/auth-provider.tsx +87 -24
- package/src/components/{email → auth/email}/email-changed.tsx +1 -1
- package/src/components/{email → auth/email}/email-verification.tsx +1 -1
- package/src/components/{email → auth/email}/magic-link.tsx +1 -1
- package/src/components/{email → auth/email}/new-device.tsx +1 -1
- package/src/components/{email → auth/email}/otp-email.tsx +1 -1
- package/src/components/{email → auth/email}/password-changed.tsx +1 -1
- package/src/components/{email → auth/email}/reset-password.tsx +1 -1
- package/src/email.ts +1 -0
- package/src/hooks/auth/use-authenticate.ts +12 -6
- package/src/hooks/auth/use-user.ts +14 -9
- package/src/hooks/use-auth-mutation.ts +30 -12
- package/src/hooks/use-auth-plugin.ts +50 -0
- package/src/hooks/use-auth-query.ts +11 -7
- package/src/index.ts +33 -32
- package/src/lib/auth-client.ts +33 -31
- package/src/lib/auth-plugin.ts +75 -0
- package/src/lib/auth-server.ts +30 -0
- package/src/mutations/auth/request-password-reset-mutation.ts +58 -0
- package/src/mutations/auth/reset-password-mutation.ts +60 -0
- package/src/mutations/auth/send-verification-email-mutation.ts +58 -0
- package/src/mutations/auth/sign-in-email-mutation.ts +78 -0
- package/src/mutations/auth/sign-in-social-mutation.ts +64 -0
- package/src/mutations/auth/sign-out-mutation.ts +71 -0
- package/src/mutations/auth/sign-up-email-mutation.ts +74 -0
- package/src/mutations/auth-mutation-options.ts +83 -38
- package/src/mutations/magic-link/sign-in-magic-link-mutation.ts +59 -0
- package/src/mutations/multi-session/revoke-multi-session-mutation.ts +70 -0
- package/src/mutations/multi-session/set-active-session-mutation.ts +98 -0
- package/src/mutations/passkey/add-passkey-mutation.ts +66 -0
- package/src/mutations/passkey/delete-passkey-mutation.ts +66 -0
- package/src/mutations/passkey/sign-in-passkey-mutation.ts +74 -0
- package/src/mutations/settings/change-email-mutation.ts +67 -0
- package/src/mutations/settings/change-password-mutation.ts +59 -0
- package/src/mutations/settings/delete-user-mutation.ts +59 -0
- package/src/mutations/settings/link-social-mutation.ts +59 -0
- package/src/mutations/settings/revoke-session-mutation.ts +69 -0
- package/src/mutations/settings/unlink-account-mutation.ts +67 -0
- package/src/mutations/settings/update-user-mutation.ts +82 -0
- package/src/mutations/username/is-username-available-mutation.ts +62 -0
- package/src/mutations/username/sign-in-username-mutation.ts +74 -0
- package/src/queries/auth/session-query.ts +135 -0
- package/src/queries/auth-query-options.ts +61 -12
- package/src/queries/multi-session/list-device-sessions-query.ts +177 -0
- package/src/queries/passkey/list-passkeys-query.ts +154 -0
- package/src/queries/settings/account-info-query.ts +158 -0
- package/src/queries/settings/list-accounts-query.ts +156 -0
- package/src/queries/settings/list-sessions-query.ts +154 -0
- package/src/server/queries/auth/session-query.ts +94 -0
- package/src/server/queries/multi-session/list-device-sessions-query.ts +102 -0
- package/src/server/queries/passkey/list-passkeys-query.ts +100 -0
- package/src/server/queries/settings/account-info-query.ts +101 -0
- package/src/server/queries/settings/list-accounts-query.ts +101 -0
- package/src/server/queries/settings/list-sessions-query.ts +101 -0
- package/src/server.ts +7 -0
- package/dist/core.d.ts +0 -1
- package/dist/core.js +0 -1
- package/dist/hooks/auth/use-is-username-available.d.ts +0 -20
- package/dist/hooks/auth/use-is-username-available.js +0 -13
- package/dist/hooks/auth/use-request-password-reset.d.ts +0 -21
- package/dist/hooks/auth/use-request-password-reset.js +0 -13
- package/dist/hooks/auth/use-reset-password.d.ts +0 -23
- package/dist/hooks/auth/use-reset-password.js +0 -13
- package/dist/hooks/auth/use-send-verification-email.d.ts +0 -19
- package/dist/hooks/auth/use-send-verification-email.js +0 -13
- package/dist/hooks/auth/use-session.d.ts +0 -35
- package/dist/hooks/auth/use-session.js +0 -16
- package/dist/hooks/auth/use-sign-in-email.d.ts +0 -26
- package/dist/hooks/auth/use-sign-in-email.js +0 -17
- package/dist/hooks/auth/use-sign-in-magic-link.d.ts +0 -27
- package/dist/hooks/auth/use-sign-in-magic-link.js +0 -13
- package/dist/hooks/auth/use-sign-in-passkey.d.ts +0 -21
- package/dist/hooks/auth/use-sign-in-passkey.js +0 -17
- package/dist/hooks/auth/use-sign-in-social.d.ts +0 -63
- package/dist/hooks/auth/use-sign-in-social.js +0 -13
- package/dist/hooks/auth/use-sign-in-username.d.ts +0 -25
- package/dist/hooks/auth/use-sign-in-username.js +0 -17
- package/dist/hooks/auth/use-sign-out.d.ts +0 -18
- package/dist/hooks/auth/use-sign-out.js +0 -16
- package/dist/hooks/auth/use-sign-up-email.d.ts +0 -1186
- package/dist/hooks/auth/use-sign-up-email.js +0 -17
- package/dist/hooks/settings/use-account-info.d.ts +0 -17
- package/dist/hooks/settings/use-account-info.js +0 -18
- package/dist/hooks/settings/use-add-passkey.d.ts +0 -21
- package/dist/hooks/settings/use-add-passkey.js +0 -17
- package/dist/hooks/settings/use-change-email.d.ts +0 -21
- package/dist/hooks/settings/use-change-email.js +0 -17
- package/dist/hooks/settings/use-change-password.d.ts +0 -21
- package/dist/hooks/settings/use-change-password.js +0 -13
- package/dist/hooks/settings/use-delete-passkey.d.ts +0 -19
- package/dist/hooks/settings/use-delete-passkey.js +0 -17
- package/dist/hooks/settings/use-delete-user.d.ts +0 -24
- package/dist/hooks/settings/use-delete-user.js +0 -13
- package/dist/hooks/settings/use-link-social.d.ts +0 -43
- package/dist/hooks/settings/use-link-social.js +0 -13
- package/dist/hooks/settings/use-list-accounts.d.ts +0 -22
- package/dist/hooks/settings/use-list-accounts.js +0 -15
- package/dist/hooks/settings/use-list-device-sessions.d.ts +0 -34
- package/dist/hooks/settings/use-list-device-sessions.js +0 -15
- package/dist/hooks/settings/use-list-sessions.d.ts +0 -23
- package/dist/hooks/settings/use-list-sessions.js +0 -15
- package/dist/hooks/settings/use-list-user-passkeys.d.ts +0 -14
- package/dist/hooks/settings/use-list-user-passkeys.js +0 -15
- package/dist/hooks/settings/use-revoke-multi-session.d.ts +0 -19
- package/dist/hooks/settings/use-revoke-multi-session.js +0 -17
- package/dist/hooks/settings/use-revoke-session.d.ts +0 -19
- package/dist/hooks/settings/use-revoke-session.js +0 -17
- package/dist/hooks/settings/use-set-active-session.d.ts +0 -20
- package/dist/hooks/settings/use-set-active-session.js +0 -20
- package/dist/hooks/settings/use-unlink-account.d.ts +0 -21
- package/dist/hooks/settings/use-unlink-account.js +0 -17
- package/dist/hooks/settings/use-update-user.d.ts +0 -1185
- package/dist/hooks/settings/use-update-user.js +0 -24
- package/dist/lib/auth-client.js +0 -11
- package/dist/lib/auth-config.d.ts +0 -43
- package/dist/lib/auth-context.d.ts +0 -2
- package/dist/lib/auth-context.js +0 -6
- package/dist/mutations/auth/is-username-available-options.d.ts +0 -19
- package/dist/mutations/auth/is-username-available-options.js +0 -7
- package/dist/mutations/auth/request-password-reset-options.d.ts +0 -15
- package/dist/mutations/auth/request-password-reset-options.js +0 -7
- package/dist/mutations/auth/reset-password-options.d.ts +0 -17
- package/dist/mutations/auth/reset-password-options.js +0 -7
- package/dist/mutations/auth/send-verification-email-options.d.ts +0 -15
- package/dist/mutations/auth/send-verification-email-options.js +0 -7
- package/dist/mutations/auth/sign-in-email-options.d.ts +0 -23
- package/dist/mutations/auth/sign-in-email-options.js +0 -11
- package/dist/mutations/auth/sign-in-magic-link-options.d.ts +0 -23
- package/dist/mutations/auth/sign-in-magic-link-options.js +0 -11
- package/dist/mutations/auth/sign-in-passkey-options.d.ts +0 -15
- package/dist/mutations/auth/sign-in-passkey-options.js +0 -11
- package/dist/mutations/auth/sign-in-social-options.d.ts +0 -61
- package/dist/mutations/auth/sign-in-social-options.js +0 -11
- package/dist/mutations/auth/sign-in-username-options.d.ts +0 -19
- package/dist/mutations/auth/sign-in-username-options.js +0 -11
- package/dist/mutations/auth/sign-out-options.d.ts +0 -12
- package/dist/mutations/auth/sign-out-options.js +0 -7
- package/dist/mutations/auth/sign-up-email-options.d.ts +0 -1180
- package/dist/mutations/auth/sign-up-email-options.js +0 -11
- package/dist/mutations/settings/add-passkey-options.d.ts +0 -15
- package/dist/mutations/settings/add-passkey-options.js +0 -11
- package/dist/mutations/settings/change-email-options.d.ts +0 -15
- package/dist/mutations/settings/change-email-options.js +0 -7
- package/dist/mutations/settings/change-password-options.d.ts +0 -17
- package/dist/mutations/settings/change-password-options.js +0 -7
- package/dist/mutations/settings/delete-passkey-options.d.ts +0 -13
- package/dist/mutations/settings/delete-passkey-options.js +0 -11
- package/dist/mutations/settings/delete-user-options.d.ts +0 -20
- package/dist/mutations/settings/delete-user-options.js +0 -7
- package/dist/mutations/settings/link-social-options.d.ts +0 -39
- package/dist/mutations/settings/link-social-options.js +0 -7
- package/dist/mutations/settings/revoke-multi-session-options.d.ts +0 -13
- package/dist/mutations/settings/revoke-multi-session-options.js +0 -11
- package/dist/mutations/settings/revoke-session-options.d.ts +0 -13
- package/dist/mutations/settings/revoke-session-options.js +0 -7
- package/dist/mutations/settings/set-active-session-options.d.ts +0 -13
- package/dist/mutations/settings/set-active-session-options.js +0 -11
- package/dist/mutations/settings/unlink-account-options.d.ts +0 -15
- package/dist/mutations/settings/unlink-account-options.js +0 -7
- package/dist/mutations/settings/update-user-options.d.ts +0 -1182
- package/dist/mutations/settings/update-user-options.js +0 -7
- package/dist/queries/auth/session-options.d.ts +0 -110
- package/dist/queries/auth/session-options.js +0 -7
- package/dist/queries/settings/account-info-options.d.ts +0 -32
- package/dist/queries/settings/account-info-options.js +0 -12
- package/dist/queries/settings/list-accounts-options.d.ts +0 -53
- package/dist/queries/settings/list-accounts-options.js +0 -12
- package/dist/queries/settings/list-device-sessions-options.d.ts +0 -99
- package/dist/queries/settings/list-device-sessions-options.js +0 -12
- package/dist/queries/settings/list-sessions-options.d.ts +0 -54
- package/dist/queries/settings/list-sessions-options.js +0 -12
- package/dist/queries/settings/list-user-passkeys-options.d.ts +0 -18
- package/dist/queries/settings/list-user-passkeys-options.js +0 -12
- package/src/core.ts +0 -1
- package/src/hooks/auth/use-is-username-available.ts +0 -31
- package/src/hooks/auth/use-request-password-reset.ts +0 -33
- package/src/hooks/auth/use-reset-password.ts +0 -31
- package/src/hooks/auth/use-send-verification-email.ts +0 -31
- package/src/hooks/auth/use-session.ts +0 -31
- package/src/hooks/auth/use-sign-in-email.ts +0 -41
- package/src/hooks/auth/use-sign-in-magic-link.ts +0 -29
- package/src/hooks/auth/use-sign-in-passkey.ts +0 -40
- package/src/hooks/auth/use-sign-in-social.ts +0 -31
- package/src/hooks/auth/use-sign-in-username.ts +0 -40
- package/src/hooks/auth/use-sign-out.ts +0 -34
- package/src/hooks/auth/use-sign-up-email.ts +0 -40
- package/src/hooks/settings/use-account-info.ts +0 -40
- package/src/hooks/settings/use-add-passkey.ts +0 -37
- package/src/hooks/settings/use-change-email.ts +0 -37
- package/src/hooks/settings/use-change-password.ts +0 -29
- package/src/hooks/settings/use-delete-passkey.ts +0 -37
- package/src/hooks/settings/use-delete-user.ts +0 -29
- package/src/hooks/settings/use-link-social.ts +0 -29
- package/src/hooks/settings/use-list-accounts.ts +0 -39
- package/src/hooks/settings/use-list-device-sessions.ts +0 -39
- package/src/hooks/settings/use-list-sessions.ts +0 -39
- package/src/hooks/settings/use-list-user-passkeys.ts +0 -39
- package/src/hooks/settings/use-revoke-multi-session.ts +0 -39
- package/src/hooks/settings/use-revoke-session.ts +0 -39
- package/src/hooks/settings/use-set-active-session.ts +0 -58
- package/src/hooks/settings/use-unlink-account.ts +0 -37
- package/src/hooks/settings/use-update-user.ts +0 -50
- package/src/lib/auth-config.ts +0 -48
- package/src/lib/auth-context.ts +0 -6
- package/src/mutations/auth/is-username-available-options.ts +0 -18
- package/src/mutations/auth/request-password-reset-options.ts +0 -14
- package/src/mutations/auth/reset-password-options.ts +0 -14
- package/src/mutations/auth/send-verification-email-options.ts +0 -14
- package/src/mutations/auth/sign-in-email-options.ts +0 -19
- package/src/mutations/auth/sign-in-magic-link-options.ts +0 -15
- package/src/mutations/auth/sign-in-passkey-options.ts +0 -15
- package/src/mutations/auth/sign-in-social-options.ts +0 -19
- package/src/mutations/auth/sign-in-username-options.ts +0 -15
- package/src/mutations/auth/sign-out-options.ts +0 -11
- package/src/mutations/auth/sign-up-email-options.ts +0 -15
- package/src/mutations/settings/add-passkey-options.ts +0 -15
- package/src/mutations/settings/change-email-options.ts +0 -11
- package/src/mutations/settings/change-password-options.ts +0 -14
- package/src/mutations/settings/delete-passkey-options.ts +0 -15
- package/src/mutations/settings/delete-user-options.ts +0 -11
- package/src/mutations/settings/link-social-options.ts +0 -11
- package/src/mutations/settings/revoke-multi-session-options.ts +0 -15
- package/src/mutations/settings/revoke-session-options.ts +0 -14
- package/src/mutations/settings/set-active-session-options.ts +0 -15
- package/src/mutations/settings/unlink-account-options.ts +0 -14
- package/src/mutations/settings/update-user-options.ts +0 -15
- package/src/queries/auth/session-options.ts +0 -18
- package/src/queries/settings/account-info-options.ts +0 -25
- package/src/queries/settings/list-accounts-options.ts +0 -26
- package/src/queries/settings/list-device-sessions-options.ts +0 -24
- package/src/queries/settings/list-sessions-options.ts +0 -23
- package/src/queries/settings/list-user-passkeys-options.ts +0 -23
- /package/dist/components/{email → auth/email}/email-changed.d.ts +0 -0
- /package/dist/components/{email → auth/email}/email-localization.d.ts +0 -0
- /package/dist/components/{email → auth/email}/email-styles.d.ts +0 -0
- /package/dist/components/{email → auth/email}/email-verification.d.ts +0 -0
- /package/dist/components/{email → auth/email}/index.d.ts +0 -0
- /package/dist/components/{email → auth/email}/index.js +0 -0
- /package/dist/components/{email → auth/email}/magic-link.d.ts +0 -0
- /package/dist/components/{email → auth/email}/new-device.d.ts +0 -0
- /package/dist/components/{email → auth/email}/otp-email.d.ts +0 -0
- /package/dist/components/{email → auth/email}/password-changed.d.ts +0 -0
- /package/dist/components/{email → auth/email}/reset-password.d.ts +0 -0
- /package/src/components/{email → auth/email}/email-localization.tsx +0 -0
- /package/src/components/{email → auth/email}/email-styles.tsx +0 -0
- /package/src/components/{email → auth/email}/index.ts +0 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { authQueryKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
type DataTag,
|
|
4
|
+
type QueryClient,
|
|
5
|
+
queryOptions,
|
|
6
|
+
skipToken,
|
|
7
|
+
useQuery
|
|
8
|
+
} from "@tanstack/react-query"
|
|
9
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
10
|
+
|
|
11
|
+
import type { InferData, MultiSessionAuthClient } from "../../lib/auth-client"
|
|
12
|
+
import { useSession } from "../auth/session-query"
|
|
13
|
+
|
|
14
|
+
export type ListDeviceSessionsData<
|
|
15
|
+
TAuthClient extends MultiSessionAuthClient = MultiSessionAuthClient
|
|
16
|
+
> = InferData<TAuthClient["multiSession"]["listDeviceSessions"]>
|
|
17
|
+
|
|
18
|
+
export type ListDeviceSession<
|
|
19
|
+
TAuthClient extends MultiSessionAuthClient = MultiSessionAuthClient
|
|
20
|
+
> = NonNullable<ListDeviceSessionsData<TAuthClient>>[number]
|
|
21
|
+
|
|
22
|
+
export type ListDeviceSessionsParams<
|
|
23
|
+
TAuthClient extends MultiSessionAuthClient
|
|
24
|
+
> = Parameters<TAuthClient["multiSession"]["listDeviceSessions"]>[0]
|
|
25
|
+
|
|
26
|
+
export type ListDeviceSessionsOptions<
|
|
27
|
+
TAuthClient extends MultiSessionAuthClient
|
|
28
|
+
> = Omit<
|
|
29
|
+
ReturnType<typeof listDeviceSessionsOptions<TAuthClient>>,
|
|
30
|
+
"queryKey" | "queryFn"
|
|
31
|
+
>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Query options factory for the current user's device sessions.
|
|
35
|
+
*
|
|
36
|
+
* @param authClient - The Better Auth client with the multi-session plugin.
|
|
37
|
+
* @param userId - The current signed-in user's ID. Used for cache partitioning.
|
|
38
|
+
* @param params - Parameters forwarded to `authClient.multiSession.listDeviceSessions`.
|
|
39
|
+
*/
|
|
40
|
+
export function listDeviceSessionsOptions<
|
|
41
|
+
TAuthClient extends MultiSessionAuthClient
|
|
42
|
+
>(
|
|
43
|
+
authClient: TAuthClient,
|
|
44
|
+
userId: string | undefined,
|
|
45
|
+
params?: ListDeviceSessionsParams<TAuthClient>
|
|
46
|
+
) {
|
|
47
|
+
type TData = ListDeviceSessionsData<TAuthClient>
|
|
48
|
+
const queryKey = authQueryKeys.listDeviceSessions(userId, params?.query)
|
|
49
|
+
|
|
50
|
+
const options = queryOptions<TData, BetterFetchError, TData, typeof queryKey>(
|
|
51
|
+
{
|
|
52
|
+
queryKey,
|
|
53
|
+
queryFn: ({ signal }) =>
|
|
54
|
+
authClient.multiSession.listDeviceSessions({
|
|
55
|
+
...params,
|
|
56
|
+
fetchOptions: { ...params?.fetchOptions, signal, throw: true }
|
|
57
|
+
}) as Promise<TData>
|
|
58
|
+
}
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
return options as typeof options & {
|
|
62
|
+
queryKey: DataTag<typeof queryKey, TData, BetterFetchError>
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get the current user's device sessions from the query cache, calling
|
|
68
|
+
* `fetchListDeviceSessions` under the hood if no cached entry exists.
|
|
69
|
+
* Resolves with the device session list, making it ideal for loaders or
|
|
70
|
+
* `beforeLoad` guards.
|
|
71
|
+
*
|
|
72
|
+
* @param queryClient - The React Query client.
|
|
73
|
+
* @param authClient - The Better Auth client with the multi-session plugin.
|
|
74
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
75
|
+
* @param params - Parameters forwarded to `authClient.multiSession.listDeviceSessions`.
|
|
76
|
+
*/
|
|
77
|
+
export const ensureListDeviceSessions = <
|
|
78
|
+
TAuthClient extends MultiSessionAuthClient
|
|
79
|
+
>(
|
|
80
|
+
queryClient: QueryClient,
|
|
81
|
+
authClient: TAuthClient,
|
|
82
|
+
userId: string,
|
|
83
|
+
params?: ListDeviceSessionsParams<TAuthClient>
|
|
84
|
+
) =>
|
|
85
|
+
queryClient.ensureQueryData(
|
|
86
|
+
listDeviceSessionsOptions(authClient, userId, params)
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Prefetch the current user's device sessions into the query cache. Behaves
|
|
91
|
+
* like `fetchListDeviceSessions`, but does not throw on error and does not
|
|
92
|
+
* return the data — use this to warm the cache without blocking navigation.
|
|
93
|
+
*
|
|
94
|
+
* @param queryClient - The React Query client.
|
|
95
|
+
* @param authClient - The Better Auth client with the multi-session plugin.
|
|
96
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
97
|
+
* @param params - Parameters forwarded to `authClient.multiSession.listDeviceSessions`.
|
|
98
|
+
*/
|
|
99
|
+
export const prefetchListDeviceSessions = <
|
|
100
|
+
TAuthClient extends MultiSessionAuthClient
|
|
101
|
+
>(
|
|
102
|
+
queryClient: QueryClient,
|
|
103
|
+
authClient: TAuthClient,
|
|
104
|
+
userId: string,
|
|
105
|
+
params?: ListDeviceSessionsParams<TAuthClient>
|
|
106
|
+
) =>
|
|
107
|
+
queryClient.prefetchQuery(
|
|
108
|
+
listDeviceSessionsOptions(authClient, userId, params)
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Fetch and cache the current user's device sessions, resolving with the
|
|
113
|
+
* data or throwing on error. If a cached entry exists and is neither
|
|
114
|
+
* invalidated nor older than `staleTime`, the cached value is returned
|
|
115
|
+
* without a network call; otherwise the latest data is fetched.
|
|
116
|
+
*
|
|
117
|
+
* @param queryClient - The React Query client.
|
|
118
|
+
* @param authClient - The Better Auth client with the multi-session plugin.
|
|
119
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
120
|
+
* @param params - Parameters forwarded to `authClient.multiSession.listDeviceSessions`.
|
|
121
|
+
*/
|
|
122
|
+
export const fetchListDeviceSessions = <
|
|
123
|
+
TAuthClient extends MultiSessionAuthClient
|
|
124
|
+
>(
|
|
125
|
+
queryClient: QueryClient,
|
|
126
|
+
authClient: TAuthClient,
|
|
127
|
+
userId: string,
|
|
128
|
+
params?: ListDeviceSessionsParams<TAuthClient>
|
|
129
|
+
) =>
|
|
130
|
+
queryClient.fetchQuery(listDeviceSessionsOptions(authClient, userId, params))
|
|
131
|
+
|
|
132
|
+
export type UseListDeviceSessionsOptions<
|
|
133
|
+
TAuthClient extends MultiSessionAuthClient
|
|
134
|
+
> = ListDeviceSessionsOptions<TAuthClient> &
|
|
135
|
+
ListDeviceSessionsParams<TAuthClient>
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Subscribe to the current user's device sessions (multi-session account
|
|
139
|
+
* switcher) via TanStack Query.
|
|
140
|
+
*
|
|
141
|
+
* Shares a query key with the server-side `listDeviceSessionsOptions`, so
|
|
142
|
+
* SSR-hydrated data is reused from the cache without an immediate refetch.
|
|
143
|
+
* The query is gated on a signed-in user; while the session is loading or
|
|
144
|
+
* absent, the underlying `queryFn` is replaced with `skipToken`.
|
|
145
|
+
*
|
|
146
|
+
* @param authClient - The Better Auth client with the multi-session plugin.
|
|
147
|
+
* @param options - `listDeviceSessions` params (`query`, `fetchOptions`)
|
|
148
|
+
* merged with `useQuery` options (e.g. `enabled`, `staleTime`, `select`).
|
|
149
|
+
* @param queryClient - Optional custom `QueryClient`. Defaults to the client
|
|
150
|
+
* from the nearest `QueryClientProvider`.
|
|
151
|
+
*/
|
|
152
|
+
export function useListDeviceSessions<
|
|
153
|
+
TAuthClient extends MultiSessionAuthClient
|
|
154
|
+
>(
|
|
155
|
+
authClient: TAuthClient,
|
|
156
|
+
options: UseListDeviceSessionsOptions<TAuthClient> = {},
|
|
157
|
+
queryClient?: QueryClient
|
|
158
|
+
) {
|
|
159
|
+
const { data: session } = useSession(authClient, undefined, queryClient)
|
|
160
|
+
const userId = session?.user.id
|
|
161
|
+
|
|
162
|
+
const { query, fetchOptions, ...queryOptions } = options
|
|
163
|
+
|
|
164
|
+
const baseOptions = listDeviceSessionsOptions(authClient, userId, {
|
|
165
|
+
query,
|
|
166
|
+
fetchOptions
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
return useQuery(
|
|
170
|
+
{
|
|
171
|
+
...queryOptions,
|
|
172
|
+
...baseOptions,
|
|
173
|
+
queryFn: userId ? baseOptions.queryFn : skipToken
|
|
174
|
+
},
|
|
175
|
+
queryClient
|
|
176
|
+
)
|
|
177
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { authQueryKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
type DataTag,
|
|
4
|
+
type QueryClient,
|
|
5
|
+
queryOptions,
|
|
6
|
+
skipToken,
|
|
7
|
+
useQuery
|
|
8
|
+
} from "@tanstack/react-query"
|
|
9
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
10
|
+
|
|
11
|
+
import type { InferData, PasskeyAuthClient } from "../../lib/auth-client"
|
|
12
|
+
import { useSession } from "../auth/session-query"
|
|
13
|
+
|
|
14
|
+
export type ListPasskeysData<TAuthClient extends PasskeyAuthClient> = InferData<
|
|
15
|
+
TAuthClient["passkey"]["listUserPasskeys"]
|
|
16
|
+
>
|
|
17
|
+
|
|
18
|
+
export type ListPasskeysParams<TAuthClient extends PasskeyAuthClient> =
|
|
19
|
+
Parameters<TAuthClient["passkey"]["listUserPasskeys"]>[0]
|
|
20
|
+
|
|
21
|
+
export type ListPasskey<
|
|
22
|
+
TAuthClient extends PasskeyAuthClient = PasskeyAuthClient
|
|
23
|
+
> = NonNullable<ListPasskeysData<TAuthClient>>[number]
|
|
24
|
+
|
|
25
|
+
export type ListPasskeysOptions<TAuthClient extends PasskeyAuthClient> = Omit<
|
|
26
|
+
ReturnType<typeof listPasskeysOptions<TAuthClient>>,
|
|
27
|
+
"queryKey" | "queryFn"
|
|
28
|
+
>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Query options factory for the current user's passkeys.
|
|
32
|
+
*
|
|
33
|
+
* @param authClient - The Better Auth client with the passkey plugin.
|
|
34
|
+
* @param userId - The current signed-in user's ID. Used for cache partitioning.
|
|
35
|
+
* @param params - Parameters forwarded to `authClient.passkey.listUserPasskeys`.
|
|
36
|
+
*/
|
|
37
|
+
export function listPasskeysOptions<TAuthClient extends PasskeyAuthClient>(
|
|
38
|
+
authClient: TAuthClient,
|
|
39
|
+
userId: string | undefined,
|
|
40
|
+
params?: ListPasskeysParams<TAuthClient>
|
|
41
|
+
) {
|
|
42
|
+
type TData = ListPasskeysData<TAuthClient>
|
|
43
|
+
const queryKey = authQueryKeys.listPasskeys(userId, params?.query)
|
|
44
|
+
|
|
45
|
+
const options = queryOptions<TData, BetterFetchError, TData, typeof queryKey>(
|
|
46
|
+
{
|
|
47
|
+
queryKey,
|
|
48
|
+
queryFn: ({ signal }) =>
|
|
49
|
+
authClient.passkey.listUserPasskeys({
|
|
50
|
+
...params,
|
|
51
|
+
fetchOptions: { ...params?.fetchOptions, signal, throw: true }
|
|
52
|
+
}) as Promise<TData>
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return options as typeof options & {
|
|
57
|
+
queryKey: DataTag<typeof queryKey, TData, BetterFetchError>
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get the current user's passkeys from the query cache, calling
|
|
63
|
+
* `fetchListPasskeys` under the hood if no cached entry exists. Resolves
|
|
64
|
+
* with the passkey list, making it ideal for loaders or `beforeLoad` guards.
|
|
65
|
+
*
|
|
66
|
+
* @param queryClient - The React Query client.
|
|
67
|
+
* @param authClient - The Better Auth client with the passkey plugin.
|
|
68
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
69
|
+
* @param params - Parameters forwarded to `authClient.passkey.listUserPasskeys`.
|
|
70
|
+
*/
|
|
71
|
+
export const ensureListPasskeys = <TAuthClient extends PasskeyAuthClient>(
|
|
72
|
+
queryClient: QueryClient,
|
|
73
|
+
authClient: TAuthClient,
|
|
74
|
+
userId: string,
|
|
75
|
+
params?: ListPasskeysParams<TAuthClient>
|
|
76
|
+
) =>
|
|
77
|
+
queryClient.ensureQueryData(listPasskeysOptions(authClient, userId, params))
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Prefetch the current user's passkeys into the query cache. Behaves like
|
|
81
|
+
* `fetchListPasskeys`, but does not throw on error and does not return
|
|
82
|
+
* the data — use this to warm the cache without blocking navigation.
|
|
83
|
+
*
|
|
84
|
+
* @param queryClient - The React Query client.
|
|
85
|
+
* @param authClient - The Better Auth client with the passkey plugin.
|
|
86
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
87
|
+
* @param params - Parameters forwarded to `authClient.passkey.listUserPasskeys`.
|
|
88
|
+
*/
|
|
89
|
+
export const prefetchListPasskeys = <TAuthClient extends PasskeyAuthClient>(
|
|
90
|
+
queryClient: QueryClient,
|
|
91
|
+
authClient: TAuthClient,
|
|
92
|
+
userId: string,
|
|
93
|
+
params?: ListPasskeysParams<TAuthClient>
|
|
94
|
+
) => queryClient.prefetchQuery(listPasskeysOptions(authClient, userId, params))
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Fetch and cache the current user's passkeys, resolving with the data or
|
|
98
|
+
* throwing on error. If a cached entry exists and is neither invalidated
|
|
99
|
+
* nor older than `staleTime`, the cached value is returned without a
|
|
100
|
+
* network call; otherwise the latest data is fetched.
|
|
101
|
+
*
|
|
102
|
+
* @param queryClient - The React Query client.
|
|
103
|
+
* @param authClient - The Better Auth client with the passkey plugin.
|
|
104
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
105
|
+
* @param params - Parameters forwarded to `authClient.passkey.listUserPasskeys`.
|
|
106
|
+
*/
|
|
107
|
+
export const fetchListPasskeys = <TAuthClient extends PasskeyAuthClient>(
|
|
108
|
+
queryClient: QueryClient,
|
|
109
|
+
authClient: TAuthClient,
|
|
110
|
+
userId: string,
|
|
111
|
+
params?: ListPasskeysParams<TAuthClient>
|
|
112
|
+
) => queryClient.fetchQuery(listPasskeysOptions(authClient, userId, params))
|
|
113
|
+
|
|
114
|
+
export type UseListPasskeysOptions<TAuthClient extends PasskeyAuthClient> =
|
|
115
|
+
ListPasskeysOptions<TAuthClient> & ListPasskeysParams<TAuthClient>
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Subscribe to the current user's passkeys via TanStack Query.
|
|
119
|
+
*
|
|
120
|
+
* Shares a query key with the server-side `listPasskeysOptions`, so
|
|
121
|
+
* SSR-hydrated data is reused from the cache without an immediate refetch.
|
|
122
|
+
* The query is gated on a signed-in user; while the session is loading or
|
|
123
|
+
* absent, the underlying `queryFn` is replaced with `skipToken`.
|
|
124
|
+
*
|
|
125
|
+
* @param authClient - The Better Auth client with the passkey plugin.
|
|
126
|
+
* @param options - `listPasskeys` params (`query`, `fetchOptions`) merged
|
|
127
|
+
* with `useQuery` options (e.g. `enabled`, `staleTime`, `select`).
|
|
128
|
+
* @param queryClient - Optional custom `QueryClient`. Defaults to the client
|
|
129
|
+
* from the nearest `QueryClientProvider`.
|
|
130
|
+
*/
|
|
131
|
+
export function useListPasskeys<TAuthClient extends PasskeyAuthClient>(
|
|
132
|
+
authClient: TAuthClient,
|
|
133
|
+
options: UseListPasskeysOptions<TAuthClient> = {},
|
|
134
|
+
queryClient?: QueryClient
|
|
135
|
+
) {
|
|
136
|
+
const { data: session } = useSession(authClient, undefined, queryClient)
|
|
137
|
+
const userId = session?.user.id
|
|
138
|
+
|
|
139
|
+
const { query, fetchOptions, ...queryOptions } = options
|
|
140
|
+
|
|
141
|
+
const baseOptions = listPasskeysOptions(authClient, userId, {
|
|
142
|
+
query,
|
|
143
|
+
fetchOptions
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
return useQuery(
|
|
147
|
+
{
|
|
148
|
+
...queryOptions,
|
|
149
|
+
...baseOptions,
|
|
150
|
+
queryFn: userId ? baseOptions.queryFn : skipToken
|
|
151
|
+
},
|
|
152
|
+
queryClient
|
|
153
|
+
)
|
|
154
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { authQueryKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
type DataTag,
|
|
4
|
+
type QueryClient,
|
|
5
|
+
queryOptions,
|
|
6
|
+
skipToken,
|
|
7
|
+
useQuery
|
|
8
|
+
} from "@tanstack/react-query"
|
|
9
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
10
|
+
|
|
11
|
+
import type { AuthClient, InferData } from "../../lib/auth-client"
|
|
12
|
+
import { useSession } from "../auth/session-query"
|
|
13
|
+
|
|
14
|
+
export type AccountInfoData<TAuthClient extends AuthClient> = InferData<
|
|
15
|
+
TAuthClient["accountInfo"]
|
|
16
|
+
>
|
|
17
|
+
|
|
18
|
+
export type AccountInfoParams<TAuthClient extends AuthClient> = Parameters<
|
|
19
|
+
TAuthClient["accountInfo"]
|
|
20
|
+
>[0]
|
|
21
|
+
|
|
22
|
+
export type AccountInfo<TAuthClient extends AuthClient = AuthClient> =
|
|
23
|
+
NonNullable<AccountInfoData<TAuthClient>>
|
|
24
|
+
|
|
25
|
+
export type AccountInfoOptions<TAuthClient extends AuthClient> = Omit<
|
|
26
|
+
ReturnType<typeof accountInfoOptions<TAuthClient>>,
|
|
27
|
+
"queryKey" | "queryFn"
|
|
28
|
+
>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Query options factory for provider-specific account info.
|
|
32
|
+
*
|
|
33
|
+
* @param authClient - The Better Auth client.
|
|
34
|
+
* @param userId - The current signed-in user's ID. Used for cache partitioning.
|
|
35
|
+
* @param params - Parameters forwarded to `authClient.accountInfo`.
|
|
36
|
+
*/
|
|
37
|
+
export function accountInfoOptions<TAuthClient extends AuthClient>(
|
|
38
|
+
authClient: TAuthClient,
|
|
39
|
+
userId: string | undefined,
|
|
40
|
+
params?: AccountInfoParams<TAuthClient>
|
|
41
|
+
) {
|
|
42
|
+
type TData = AccountInfoData<TAuthClient>
|
|
43
|
+
const queryKey = authQueryKeys.accountInfo(userId, params?.query)
|
|
44
|
+
|
|
45
|
+
const options = queryOptions<TData, BetterFetchError, TData, typeof queryKey>(
|
|
46
|
+
{
|
|
47
|
+
queryKey,
|
|
48
|
+
queryFn: ({ signal }) =>
|
|
49
|
+
authClient.accountInfo({
|
|
50
|
+
...params,
|
|
51
|
+
fetchOptions: { ...params?.fetchOptions, signal, throw: true }
|
|
52
|
+
}) as Promise<TData>
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return options as typeof options & {
|
|
57
|
+
queryKey: DataTag<typeof queryKey, TData, BetterFetchError>
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get the current user's provider-specific account info from the query
|
|
63
|
+
* cache, calling `fetchAccountInfo` under the hood if no cached entry
|
|
64
|
+
* exists. Resolves with the data, making it ideal for loaders or
|
|
65
|
+
* `beforeLoad` guards.
|
|
66
|
+
*
|
|
67
|
+
* @param queryClient - The React Query client.
|
|
68
|
+
* @param authClient - The Better Auth client.
|
|
69
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
70
|
+
* @param params - Parameters forwarded to `authClient.accountInfo`.
|
|
71
|
+
*/
|
|
72
|
+
export const ensureAccountInfo = <TAuthClient extends AuthClient>(
|
|
73
|
+
queryClient: QueryClient,
|
|
74
|
+
authClient: TAuthClient,
|
|
75
|
+
userId: string,
|
|
76
|
+
params?: AccountInfoParams<TAuthClient>
|
|
77
|
+
) => queryClient.ensureQueryData(accountInfoOptions(authClient, userId, params))
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Prefetch the current user's provider-specific account info into the query
|
|
81
|
+
* cache. Behaves like `fetchAccountInfo`, but does not throw on error and
|
|
82
|
+
* does not return the data — use this to warm the cache without blocking
|
|
83
|
+
* navigation.
|
|
84
|
+
*
|
|
85
|
+
* @param queryClient - The React Query client.
|
|
86
|
+
* @param authClient - The Better Auth client.
|
|
87
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
88
|
+
* @param params - Parameters forwarded to `authClient.accountInfo`.
|
|
89
|
+
*/
|
|
90
|
+
export const prefetchAccountInfo = <TAuthClient extends AuthClient>(
|
|
91
|
+
queryClient: QueryClient,
|
|
92
|
+
authClient: TAuthClient,
|
|
93
|
+
userId: string,
|
|
94
|
+
params?: AccountInfoParams<TAuthClient>
|
|
95
|
+
) => queryClient.prefetchQuery(accountInfoOptions(authClient, userId, params))
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Fetch and cache the current user's provider-specific account info,
|
|
99
|
+
* resolving with the data or throwing on error. If a cached entry exists
|
|
100
|
+
* and is neither invalidated nor older than `staleTime`, the cached value
|
|
101
|
+
* is returned without a network call; otherwise the latest data is fetched.
|
|
102
|
+
*
|
|
103
|
+
* @param queryClient - The React Query client.
|
|
104
|
+
* @param authClient - The Better Auth client.
|
|
105
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
106
|
+
* @param params - Parameters forwarded to `authClient.accountInfo`.
|
|
107
|
+
*/
|
|
108
|
+
export const fetchAccountInfo = <TAuthClient extends AuthClient>(
|
|
109
|
+
queryClient: QueryClient,
|
|
110
|
+
authClient: TAuthClient,
|
|
111
|
+
userId: string,
|
|
112
|
+
params?: AccountInfoParams<TAuthClient>
|
|
113
|
+
) => queryClient.fetchQuery(accountInfoOptions(authClient, userId, params))
|
|
114
|
+
|
|
115
|
+
export type UseAccountInfoOptions<TAuthClient extends AuthClient> =
|
|
116
|
+
AccountInfoOptions<TAuthClient> & AccountInfoParams<TAuthClient>
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Subscribe to provider-specific info for a linked account via TanStack Query.
|
|
120
|
+
*
|
|
121
|
+
* Shares a query key with the server-side `accountInfoOptions`, so
|
|
122
|
+
* SSR-hydrated data is reused from the cache without an immediate refetch.
|
|
123
|
+
* The query is gated on both a signed-in user and a resolved `accountId`;
|
|
124
|
+
* until both are present, the underlying `queryFn` is replaced with
|
|
125
|
+
* `skipToken`.
|
|
126
|
+
*
|
|
127
|
+
* @param authClient - The Better Auth client.
|
|
128
|
+
* @param options - `accountInfo` params (`query`, `fetchOptions`) merged
|
|
129
|
+
* with `useQuery` options (e.g. `enabled`, `staleTime`, `select`).
|
|
130
|
+
* @param queryClient - Optional custom `QueryClient`. Defaults to the client
|
|
131
|
+
* from the nearest `QueryClientProvider`.
|
|
132
|
+
*/
|
|
133
|
+
export function useAccountInfo<TAuthClient extends AuthClient>(
|
|
134
|
+
authClient: TAuthClient,
|
|
135
|
+
options: UseAccountInfoOptions<TAuthClient> = {},
|
|
136
|
+
queryClient?: QueryClient
|
|
137
|
+
) {
|
|
138
|
+
const { data: session } = useSession(authClient, undefined, queryClient)
|
|
139
|
+
const userId = session?.user.id
|
|
140
|
+
|
|
141
|
+
const { query, fetchOptions, ...queryOptions } = options
|
|
142
|
+
|
|
143
|
+
const baseOptions = accountInfoOptions(authClient, userId, {
|
|
144
|
+
query,
|
|
145
|
+
fetchOptions
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const canFetch = Boolean(userId && query?.accountId)
|
|
149
|
+
|
|
150
|
+
return useQuery(
|
|
151
|
+
{
|
|
152
|
+
...queryOptions,
|
|
153
|
+
...baseOptions,
|
|
154
|
+
queryFn: canFetch ? baseOptions.queryFn : skipToken
|
|
155
|
+
},
|
|
156
|
+
queryClient
|
|
157
|
+
)
|
|
158
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { authQueryKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
type DataTag,
|
|
4
|
+
type QueryClient,
|
|
5
|
+
queryOptions,
|
|
6
|
+
skipToken,
|
|
7
|
+
useQuery
|
|
8
|
+
} from "@tanstack/react-query"
|
|
9
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
10
|
+
|
|
11
|
+
import type { AuthClient, InferData } from "../../lib/auth-client"
|
|
12
|
+
import { useSession } from "../auth/session-query"
|
|
13
|
+
|
|
14
|
+
export type ListAccountsData<TAuthClient extends AuthClient> = InferData<
|
|
15
|
+
TAuthClient["listAccounts"]
|
|
16
|
+
>
|
|
17
|
+
|
|
18
|
+
export type ListAccountsParams<TAuthClient extends AuthClient> = Parameters<
|
|
19
|
+
TAuthClient["listAccounts"]
|
|
20
|
+
>[0]
|
|
21
|
+
|
|
22
|
+
export type ListAccount<TAuthClient extends AuthClient = AuthClient> =
|
|
23
|
+
NonNullable<ListAccountsData<TAuthClient>>[number]
|
|
24
|
+
|
|
25
|
+
export type ListAccountsOptions<TAuthClient extends AuthClient> = Omit<
|
|
26
|
+
ReturnType<typeof listAccountsOptions<TAuthClient>>,
|
|
27
|
+
"queryKey" | "queryFn"
|
|
28
|
+
>
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Query options factory for a user's linked social accounts.
|
|
32
|
+
*
|
|
33
|
+
* @param authClient - The Better Auth client.
|
|
34
|
+
* @param userId - The current signed-in user's ID. Used for cache partitioning.
|
|
35
|
+
* @param params - Parameters forwarded to `authClient.listAccounts`.
|
|
36
|
+
*/
|
|
37
|
+
export function listAccountsOptions<TAuthClient extends AuthClient>(
|
|
38
|
+
authClient: TAuthClient,
|
|
39
|
+
userId: string | undefined,
|
|
40
|
+
params?: ListAccountsParams<TAuthClient>
|
|
41
|
+
) {
|
|
42
|
+
type TData = ListAccountsData<TAuthClient>
|
|
43
|
+
const queryKey = authQueryKeys.listAccounts(userId, params?.query)
|
|
44
|
+
|
|
45
|
+
const options = queryOptions<TData, BetterFetchError, TData, typeof queryKey>(
|
|
46
|
+
{
|
|
47
|
+
queryKey,
|
|
48
|
+
queryFn: ({ signal }) =>
|
|
49
|
+
authClient.listAccounts({
|
|
50
|
+
...params,
|
|
51
|
+
fetchOptions: { ...params?.fetchOptions, signal, throw: true }
|
|
52
|
+
}) as Promise<TData>
|
|
53
|
+
}
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return options as typeof options & {
|
|
57
|
+
queryKey: DataTag<typeof queryKey, TData, BetterFetchError>
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Get the current user's linked social accounts from the query cache,
|
|
63
|
+
* calling `fetchListAccounts` under the hood if no cached entry exists.
|
|
64
|
+
* Resolves with the account list, making it ideal for loaders or
|
|
65
|
+
* `beforeLoad` guards.
|
|
66
|
+
*
|
|
67
|
+
* @param queryClient - The React Query client.
|
|
68
|
+
* @param authClient - The Better Auth client.
|
|
69
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
70
|
+
* @param params - Parameters forwarded to `authClient.listAccounts`.
|
|
71
|
+
*/
|
|
72
|
+
export const ensureListAccounts = <TAuthClient extends AuthClient>(
|
|
73
|
+
queryClient: QueryClient,
|
|
74
|
+
authClient: TAuthClient,
|
|
75
|
+
userId: string,
|
|
76
|
+
params?: ListAccountsParams<TAuthClient>
|
|
77
|
+
) =>
|
|
78
|
+
queryClient.ensureQueryData(listAccountsOptions(authClient, userId, params))
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Prefetch the current user's linked social accounts into the query cache.
|
|
82
|
+
* Behaves like `fetchListAccounts`, but does not throw on error and does
|
|
83
|
+
* not return the data — use this to warm the cache without blocking
|
|
84
|
+
* navigation.
|
|
85
|
+
*
|
|
86
|
+
* @param queryClient - The React Query client.
|
|
87
|
+
* @param authClient - The Better Auth client.
|
|
88
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
89
|
+
* @param params - Parameters forwarded to `authClient.listAccounts`.
|
|
90
|
+
*/
|
|
91
|
+
export const prefetchListAccounts = <TAuthClient extends AuthClient>(
|
|
92
|
+
queryClient: QueryClient,
|
|
93
|
+
authClient: TAuthClient,
|
|
94
|
+
userId: string,
|
|
95
|
+
params?: ListAccountsParams<TAuthClient>
|
|
96
|
+
) => queryClient.prefetchQuery(listAccountsOptions(authClient, userId, params))
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Fetch and cache the current user's linked social accounts, resolving
|
|
100
|
+
* with the data or throwing on error. If a cached entry exists and is
|
|
101
|
+
* neither invalidated nor older than `staleTime`, the cached value is
|
|
102
|
+
* returned without a network call; otherwise the latest data is fetched.
|
|
103
|
+
*
|
|
104
|
+
* @param queryClient - The React Query client.
|
|
105
|
+
* @param authClient - The Better Auth client.
|
|
106
|
+
* @param userId - The signed-in user's ID, used for cache partitioning.
|
|
107
|
+
* @param params - Parameters forwarded to `authClient.listAccounts`.
|
|
108
|
+
*/
|
|
109
|
+
export const fetchListAccounts = <TAuthClient extends AuthClient>(
|
|
110
|
+
queryClient: QueryClient,
|
|
111
|
+
authClient: TAuthClient,
|
|
112
|
+
userId: string,
|
|
113
|
+
params?: ListAccountsParams<TAuthClient>
|
|
114
|
+
) => queryClient.fetchQuery(listAccountsOptions(authClient, userId, params))
|
|
115
|
+
|
|
116
|
+
export type UseListAccountsOptions<TAuthClient extends AuthClient> =
|
|
117
|
+
ListAccountsOptions<TAuthClient> & ListAccountsParams<TAuthClient>
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Subscribe to the current user's linked social accounts via TanStack Query.
|
|
121
|
+
*
|
|
122
|
+
* Shares a query key with the server-side `listAccountsOptions`, so
|
|
123
|
+
* SSR-hydrated data is reused from the cache without an immediate refetch.
|
|
124
|
+
* The query is gated on a signed-in user; while the session is loading or
|
|
125
|
+
* absent, the underlying `queryFn` is replaced with `skipToken`.
|
|
126
|
+
*
|
|
127
|
+
* @param authClient - The Better Auth client.
|
|
128
|
+
* @param options - `listAccounts` params (`query`, `fetchOptions`) merged
|
|
129
|
+
* with `useQuery` options (e.g. `enabled`, `staleTime`, `select`).
|
|
130
|
+
* @param queryClient - Optional custom `QueryClient`. Defaults to the client
|
|
131
|
+
* from the nearest `QueryClientProvider`.
|
|
132
|
+
*/
|
|
133
|
+
export function useListAccounts<TAuthClient extends AuthClient>(
|
|
134
|
+
authClient: TAuthClient,
|
|
135
|
+
options: UseListAccountsOptions<TAuthClient> = {},
|
|
136
|
+
queryClient?: QueryClient
|
|
137
|
+
) {
|
|
138
|
+
const { data: session } = useSession(authClient, undefined, queryClient)
|
|
139
|
+
const userId = session?.user.id
|
|
140
|
+
|
|
141
|
+
const { query, fetchOptions, ...queryOptions } = options
|
|
142
|
+
|
|
143
|
+
const baseOptions = listAccountsOptions(authClient, userId, {
|
|
144
|
+
query,
|
|
145
|
+
fetchOptions
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
return useQuery(
|
|
149
|
+
{
|
|
150
|
+
...queryOptions,
|
|
151
|
+
...baseOptions,
|
|
152
|
+
queryFn: userId ? baseOptions.queryFn : skipToken
|
|
153
|
+
},
|
|
154
|
+
queryClient
|
|
155
|
+
)
|
|
156
|
+
}
|