@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,69 @@
|
|
|
1
|
+
import { authMutationKeys } from "@better-auth-ui/core"
|
|
2
|
+
import { mutationOptions, useMutation } from "@tanstack/react-query"
|
|
3
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
4
|
+
|
|
5
|
+
import type { AuthClient } from "../../lib/auth-client"
|
|
6
|
+
import { useListSessions } from "../../queries/settings/list-sessions-query"
|
|
7
|
+
|
|
8
|
+
export type RevokeSessionParams<TAuthClient extends AuthClient> = Parameters<
|
|
9
|
+
TAuthClient["revokeSession"]
|
|
10
|
+
>[0]
|
|
11
|
+
|
|
12
|
+
export type RevokeSessionOptions<TAuthClient extends AuthClient> = Omit<
|
|
13
|
+
ReturnType<typeof revokeSessionOptions<TAuthClient>>,
|
|
14
|
+
"mutationKey" | "mutationFn"
|
|
15
|
+
>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Mutation options factory for revoking a user session.
|
|
19
|
+
*
|
|
20
|
+
* @param authClient - The Better Auth client.
|
|
21
|
+
*/
|
|
22
|
+
export function revokeSessionOptions<TAuthClient extends AuthClient>(
|
|
23
|
+
authClient: TAuthClient
|
|
24
|
+
) {
|
|
25
|
+
const mutationKey = authMutationKeys.revokeSession
|
|
26
|
+
|
|
27
|
+
const mutationFn = (params: RevokeSessionParams<TAuthClient>) =>
|
|
28
|
+
authClient.revokeSession({
|
|
29
|
+
...params,
|
|
30
|
+
fetchOptions: { ...params?.fetchOptions, throw: true }
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
return mutationOptions<
|
|
34
|
+
Awaited<ReturnType<typeof mutationFn>>,
|
|
35
|
+
BetterFetchError,
|
|
36
|
+
Parameters<typeof mutationFn>[0]
|
|
37
|
+
>({
|
|
38
|
+
mutationKey,
|
|
39
|
+
mutationFn
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a mutation for revoking a user session.
|
|
45
|
+
*
|
|
46
|
+
* Wraps `authClient.revokeSession`, refetches the sessions list on success,
|
|
47
|
+
* and forwards React Query mutation options such as `onSuccess`, `onError`,
|
|
48
|
+
* and `retry`.
|
|
49
|
+
*
|
|
50
|
+
* @param authClient - The Better Auth client.
|
|
51
|
+
* @param options - React Query options forwarded to `useMutation`.
|
|
52
|
+
*/
|
|
53
|
+
export function useRevokeSession<TAuthClient extends AuthClient>(
|
|
54
|
+
authClient: TAuthClient,
|
|
55
|
+
options?: RevokeSessionOptions<TAuthClient>
|
|
56
|
+
) {
|
|
57
|
+
const { refetch: refetchSessions } = useListSessions(authClient, {
|
|
58
|
+
refetchOnMount: false
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
return useMutation({
|
|
62
|
+
...revokeSessionOptions(authClient),
|
|
63
|
+
...options,
|
|
64
|
+
onSuccess: async (...args) => {
|
|
65
|
+
await refetchSessions()
|
|
66
|
+
await options?.onSuccess?.(...args)
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { authMutationKeys } from "@better-auth-ui/core"
|
|
2
|
+
import { mutationOptions, useMutation } from "@tanstack/react-query"
|
|
3
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
4
|
+
|
|
5
|
+
import type { AuthClient } from "../../lib/auth-client"
|
|
6
|
+
import { useListAccounts } from "../../queries/settings/list-accounts-query"
|
|
7
|
+
|
|
8
|
+
export type UnlinkAccountParams<TAuthClient extends AuthClient> = Parameters<
|
|
9
|
+
TAuthClient["unlinkAccount"]
|
|
10
|
+
>[0]
|
|
11
|
+
|
|
12
|
+
export type UnlinkAccountOptions<TAuthClient extends AuthClient> = Omit<
|
|
13
|
+
ReturnType<typeof unlinkAccountOptions<TAuthClient>>,
|
|
14
|
+
"mutationKey" | "mutationFn"
|
|
15
|
+
>
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Mutation options factory for unlinking a social provider from the current user.
|
|
19
|
+
*
|
|
20
|
+
* @param authClient - The Better Auth client.
|
|
21
|
+
*/
|
|
22
|
+
export function unlinkAccountOptions<TAuthClient extends AuthClient>(
|
|
23
|
+
authClient: TAuthClient
|
|
24
|
+
) {
|
|
25
|
+
const mutationKey = authMutationKeys.unlinkAccount
|
|
26
|
+
|
|
27
|
+
const mutationFn = (params: UnlinkAccountParams<TAuthClient>) =>
|
|
28
|
+
authClient.unlinkAccount({
|
|
29
|
+
...params,
|
|
30
|
+
fetchOptions: { ...params?.fetchOptions, throw: true }
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
return mutationOptions<
|
|
34
|
+
Awaited<ReturnType<typeof mutationFn>>,
|
|
35
|
+
BetterFetchError,
|
|
36
|
+
Parameters<typeof mutationFn>[0]
|
|
37
|
+
>({
|
|
38
|
+
mutationKey,
|
|
39
|
+
mutationFn
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Create a mutation for unlinking a social provider from the current user.
|
|
45
|
+
*
|
|
46
|
+
* Wraps `authClient.unlinkAccount`, refetches the linked accounts list on
|
|
47
|
+
* success, and forwards React Query mutation options such as `onSuccess`,
|
|
48
|
+
* `onError`, and `retry`.
|
|
49
|
+
*
|
|
50
|
+
* @param authClient - The Better Auth client.
|
|
51
|
+
* @param options - React Query options forwarded to `useMutation`.
|
|
52
|
+
*/
|
|
53
|
+
export function useUnlinkAccount<TAuthClient extends AuthClient>(
|
|
54
|
+
authClient: TAuthClient,
|
|
55
|
+
options?: UnlinkAccountOptions<TAuthClient>
|
|
56
|
+
) {
|
|
57
|
+
const { refetch } = useListAccounts(authClient, { refetchOnMount: false })
|
|
58
|
+
|
|
59
|
+
return useMutation({
|
|
60
|
+
...unlinkAccountOptions(authClient),
|
|
61
|
+
...options,
|
|
62
|
+
onSuccess: async (...args) => {
|
|
63
|
+
await refetch()
|
|
64
|
+
await options?.onSuccess?.(...args)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { authMutationKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
mutationOptions,
|
|
4
|
+
useMutation,
|
|
5
|
+
useQueryClient
|
|
6
|
+
} from "@tanstack/react-query"
|
|
7
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
8
|
+
import type { AuthClient } from "../../lib/auth-client"
|
|
9
|
+
import { sessionOptions, useSession } from "../../queries/auth/session-query"
|
|
10
|
+
|
|
11
|
+
export type UpdateUserParams<TAuthClient extends AuthClient> = Parameters<
|
|
12
|
+
TAuthClient["updateUser"]
|
|
13
|
+
>[0]
|
|
14
|
+
|
|
15
|
+
export type UpdateUserOptions<TAuthClient extends AuthClient> = Omit<
|
|
16
|
+
ReturnType<typeof updateUserOptions<TAuthClient>>,
|
|
17
|
+
"mutationKey" | "mutationFn"
|
|
18
|
+
>
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Mutation options factory for updating the authenticated user's profile.
|
|
22
|
+
*
|
|
23
|
+
* @param authClient - The Better Auth client.
|
|
24
|
+
*/
|
|
25
|
+
export function updateUserOptions<TAuthClient extends AuthClient>(
|
|
26
|
+
authClient: TAuthClient
|
|
27
|
+
) {
|
|
28
|
+
const mutationKey = authMutationKeys.updateUser
|
|
29
|
+
|
|
30
|
+
const mutationFn = (params: UpdateUserParams<TAuthClient>) =>
|
|
31
|
+
authClient.updateUser({
|
|
32
|
+
...params,
|
|
33
|
+
fetchOptions: { ...params?.fetchOptions, throw: true }
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
return mutationOptions<
|
|
37
|
+
Awaited<ReturnType<typeof mutationFn>>,
|
|
38
|
+
BetterFetchError,
|
|
39
|
+
Parameters<typeof mutationFn>[0]
|
|
40
|
+
>({
|
|
41
|
+
mutationKey,
|
|
42
|
+
mutationFn
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create a mutation for updating the authenticated user's profile.
|
|
48
|
+
*
|
|
49
|
+
* Wraps `authClient.updateUser`, optimistically patches the cached session
|
|
50
|
+
* with the new fields, refetches the session, and forwards React Query
|
|
51
|
+
* mutation options such as `onSuccess`, `onError`, and `retry`.
|
|
52
|
+
*
|
|
53
|
+
* @param authClient - The Better Auth client.
|
|
54
|
+
* @param options - React Query options forwarded to `useMutation`.
|
|
55
|
+
*/
|
|
56
|
+
export function useUpdateUser<TAuthClient extends AuthClient>(
|
|
57
|
+
authClient: TAuthClient,
|
|
58
|
+
options?: UpdateUserOptions<TAuthClient>
|
|
59
|
+
) {
|
|
60
|
+
const { data: session, refetch: refetchSession } = useSession(authClient, {
|
|
61
|
+
refetchOnMount: false
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const queryClient = useQueryClient()
|
|
65
|
+
|
|
66
|
+
return useMutation({
|
|
67
|
+
...updateUserOptions(authClient),
|
|
68
|
+
...options,
|
|
69
|
+
onSuccess: async (data, variables, ...rest) => {
|
|
70
|
+
if (session) {
|
|
71
|
+
queryClient.setQueryData(sessionOptions(authClient).queryKey, {
|
|
72
|
+
...session,
|
|
73
|
+
user: { ...session.user, ...variables }
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
refetchSession()
|
|
78
|
+
|
|
79
|
+
await options?.onSuccess?.(data, variables, ...rest)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { authMutationKeys } from "@better-auth-ui/core"
|
|
2
|
+
import { mutationOptions, useMutation } from "@tanstack/react-query"
|
|
3
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
4
|
+
|
|
5
|
+
import type { UsernameAuthClient } from "../../lib/auth-client"
|
|
6
|
+
|
|
7
|
+
export type IsUsernameAvailableParams<TAuthClient extends UsernameAuthClient> =
|
|
8
|
+
Parameters<TAuthClient["isUsernameAvailable"]>[0]
|
|
9
|
+
|
|
10
|
+
export type IsUsernameAvailableOptions<TAuthClient extends UsernameAuthClient> =
|
|
11
|
+
Omit<
|
|
12
|
+
ReturnType<typeof isUsernameAvailableOptions<TAuthClient>>,
|
|
13
|
+
"mutationKey" | "mutationFn"
|
|
14
|
+
>
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Mutation options factory for checking username availability.
|
|
18
|
+
*
|
|
19
|
+
* Modeled as a mutation because callers typically trigger the check on
|
|
20
|
+
* user action (debounced input, form submit) rather than on mount.
|
|
21
|
+
*
|
|
22
|
+
* @param authClient - The Better Auth client.
|
|
23
|
+
*/
|
|
24
|
+
export function isUsernameAvailableOptions<
|
|
25
|
+
TAuthClient extends UsernameAuthClient
|
|
26
|
+
>(authClient: TAuthClient) {
|
|
27
|
+
const mutationKey = authMutationKeys.isUsernameAvailable
|
|
28
|
+
|
|
29
|
+
const mutationFn = (params: IsUsernameAvailableParams<TAuthClient>) =>
|
|
30
|
+
authClient.isUsernameAvailable({
|
|
31
|
+
...params,
|
|
32
|
+
fetchOptions: { ...params?.fetchOptions, throw: true }
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return mutationOptions<
|
|
36
|
+
Awaited<ReturnType<typeof mutationFn>>,
|
|
37
|
+
BetterFetchError,
|
|
38
|
+
Parameters<typeof mutationFn>[0]
|
|
39
|
+
>({
|
|
40
|
+
mutationKey,
|
|
41
|
+
mutationFn
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Create a mutation for checking whether a username is available.
|
|
47
|
+
*
|
|
48
|
+
* Wraps `authClient.isUsernameAvailable` and forwards React Query mutation
|
|
49
|
+
* options such as `onSuccess`, `onError`, and `retry`.
|
|
50
|
+
*
|
|
51
|
+
* @param authClient - The Better Auth client with the username plugin.
|
|
52
|
+
* @param options - React Query options forwarded to `useMutation`.
|
|
53
|
+
*/
|
|
54
|
+
export function useIsUsernameAvailable<TAuthClient extends UsernameAuthClient>(
|
|
55
|
+
authClient: TAuthClient,
|
|
56
|
+
options?: IsUsernameAvailableOptions<TAuthClient>
|
|
57
|
+
) {
|
|
58
|
+
return useMutation({
|
|
59
|
+
...isUsernameAvailableOptions(authClient),
|
|
60
|
+
...options
|
|
61
|
+
})
|
|
62
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { authMutationKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
mutationOptions,
|
|
4
|
+
useMutation,
|
|
5
|
+
useQueryClient
|
|
6
|
+
} from "@tanstack/react-query"
|
|
7
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
8
|
+
|
|
9
|
+
import type { UsernameAuthClient } from "../../lib/auth-client"
|
|
10
|
+
import { sessionOptions } from "../../queries/auth/session-query"
|
|
11
|
+
|
|
12
|
+
export type SignInUsernameParams<TAuthClient extends UsernameAuthClient> =
|
|
13
|
+
Parameters<TAuthClient["signIn"]["username"]>[0]
|
|
14
|
+
|
|
15
|
+
export type SignInUsernameOptions<TAuthClient extends UsernameAuthClient> =
|
|
16
|
+
Omit<
|
|
17
|
+
ReturnType<typeof signInUsernameOptions<TAuthClient>>,
|
|
18
|
+
"mutationKey" | "mutationFn"
|
|
19
|
+
>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Mutation options factory for username/password sign-in.
|
|
23
|
+
*
|
|
24
|
+
* @param authClient - The Better Auth client.
|
|
25
|
+
*/
|
|
26
|
+
export function signInUsernameOptions<TAuthClient extends UsernameAuthClient>(
|
|
27
|
+
authClient: TAuthClient
|
|
28
|
+
) {
|
|
29
|
+
const mutationKey = authMutationKeys.signIn.username
|
|
30
|
+
|
|
31
|
+
const mutationFn = (params: SignInUsernameParams<TAuthClient>) =>
|
|
32
|
+
authClient.signIn.username({
|
|
33
|
+
...params,
|
|
34
|
+
fetchOptions: { ...params?.fetchOptions, throw: true }
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
return mutationOptions<
|
|
38
|
+
Awaited<ReturnType<typeof mutationFn>>,
|
|
39
|
+
BetterFetchError,
|
|
40
|
+
Parameters<typeof mutationFn>[0]
|
|
41
|
+
>({
|
|
42
|
+
mutationKey,
|
|
43
|
+
mutationFn
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Create a mutation for username/password sign-in.
|
|
49
|
+
*
|
|
50
|
+
* Wraps `authClient.signIn.username`, resets the session query on success so
|
|
51
|
+
* the new session is refetched, and forwards React Query mutation options
|
|
52
|
+
* such as `onSuccess`, `onError`, and `retry`.
|
|
53
|
+
*
|
|
54
|
+
* @param authClient - The Better Auth client with the username plugin.
|
|
55
|
+
* @param options - React Query options forwarded to `useMutation`.
|
|
56
|
+
*/
|
|
57
|
+
export function useSignInUsername<TAuthClient extends UsernameAuthClient>(
|
|
58
|
+
authClient: TAuthClient,
|
|
59
|
+
options?: SignInUsernameOptions<TAuthClient>
|
|
60
|
+
) {
|
|
61
|
+
const queryClient = useQueryClient()
|
|
62
|
+
|
|
63
|
+
return useMutation({
|
|
64
|
+
...signInUsernameOptions(authClient),
|
|
65
|
+
...options,
|
|
66
|
+
onSuccess: async (...args) => {
|
|
67
|
+
queryClient.resetQueries({
|
|
68
|
+
queryKey: sessionOptions(authClient).queryKey
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
await options?.onSuccess?.(...args)
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { authQueryKeys } from "@better-auth-ui/core"
|
|
2
|
+
import {
|
|
3
|
+
type DataTag,
|
|
4
|
+
type QueryClient,
|
|
5
|
+
queryOptions,
|
|
6
|
+
useQuery
|
|
7
|
+
} from "@tanstack/react-query"
|
|
8
|
+
import type { BetterFetchError } from "better-auth/react"
|
|
9
|
+
|
|
10
|
+
import type { AuthClient, InferData } from "../../lib/auth-client"
|
|
11
|
+
|
|
12
|
+
export type SessionData<TAuthClient extends AuthClient = AuthClient> =
|
|
13
|
+
InferData<TAuthClient["getSession"]>
|
|
14
|
+
|
|
15
|
+
export type Session<TAuthClient extends AuthClient = AuthClient> = NonNullable<
|
|
16
|
+
SessionData<TAuthClient>
|
|
17
|
+
>
|
|
18
|
+
|
|
19
|
+
export type SessionParams<TAuthClient extends AuthClient> = Parameters<
|
|
20
|
+
TAuthClient["getSession"]
|
|
21
|
+
>[0]
|
|
22
|
+
|
|
23
|
+
export type SessionOptions<TAuthClient extends AuthClient> = Omit<
|
|
24
|
+
ReturnType<typeof sessionOptions<TAuthClient>>,
|
|
25
|
+
"queryKey" | "queryFn"
|
|
26
|
+
>
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Query options factory for the current session.
|
|
30
|
+
*
|
|
31
|
+
* Shares a query key with the server-side `sessionOptions` so that data
|
|
32
|
+
* prefetched during SSR hydrates seamlessly into the client cache.
|
|
33
|
+
*
|
|
34
|
+
* @param authClient - The Better Auth client.
|
|
35
|
+
* @param params - Parameters forwarded to `authClient.getSession`.
|
|
36
|
+
*/
|
|
37
|
+
export function sessionOptions<TAuthClient extends AuthClient>(
|
|
38
|
+
authClient: TAuthClient,
|
|
39
|
+
params?: SessionParams<TAuthClient>
|
|
40
|
+
) {
|
|
41
|
+
type TData = SessionData<TAuthClient>
|
|
42
|
+
const queryKey = authQueryKeys.session
|
|
43
|
+
|
|
44
|
+
const options = queryOptions<TData, BetterFetchError, TData, typeof queryKey>(
|
|
45
|
+
{
|
|
46
|
+
queryKey,
|
|
47
|
+
queryFn: ({ signal }) =>
|
|
48
|
+
authClient.getSession({
|
|
49
|
+
...params,
|
|
50
|
+
fetchOptions: { ...params?.fetchOptions, signal, throw: true }
|
|
51
|
+
}) as Promise<TData>
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return options as typeof options & {
|
|
56
|
+
queryKey: DataTag<typeof queryKey, TData, BetterFetchError>
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get the current session from the query cache, calling `fetchSession` under
|
|
62
|
+
* the hood if no cached entry exists. Resolves with the session data, making
|
|
63
|
+
* it ideal for loaders or `beforeLoad` guards.
|
|
64
|
+
*
|
|
65
|
+
* @param queryClient - The React Query client.
|
|
66
|
+
* @param authClient - The Better Auth client.
|
|
67
|
+
* @param params - Parameters forwarded to `authClient.getSession`.
|
|
68
|
+
*/
|
|
69
|
+
export const ensureSession = <TAuthClient extends AuthClient>(
|
|
70
|
+
queryClient: QueryClient,
|
|
71
|
+
authClient: TAuthClient,
|
|
72
|
+
params?: SessionParams<TAuthClient>
|
|
73
|
+
) => queryClient.ensureQueryData(sessionOptions(authClient, params))
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Prefetch the current session into the query cache. Behaves like
|
|
77
|
+
* `fetchSession`, but does not throw on error and does not return the data —
|
|
78
|
+
* use this to warm the cache without blocking navigation.
|
|
79
|
+
*
|
|
80
|
+
* @param queryClient - The React Query client.
|
|
81
|
+
* @param authClient - The Better Auth client.
|
|
82
|
+
* @param params - Parameters forwarded to `authClient.getSession`.
|
|
83
|
+
*/
|
|
84
|
+
export const prefetchSession = <TAuthClient extends AuthClient>(
|
|
85
|
+
queryClient: QueryClient,
|
|
86
|
+
authClient: TAuthClient,
|
|
87
|
+
params?: SessionParams<TAuthClient>
|
|
88
|
+
) => queryClient.prefetchQuery(sessionOptions(authClient, params))
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Fetch and cache the current session, resolving with the data or throwing
|
|
92
|
+
* on error. If a cached entry exists and is neither invalidated nor older
|
|
93
|
+
* than `staleTime`, the cached value is returned without a network call;
|
|
94
|
+
* otherwise the latest data is fetched.
|
|
95
|
+
*
|
|
96
|
+
* @param queryClient - The React Query client.
|
|
97
|
+
* @param authClient - The Better Auth client.
|
|
98
|
+
* @param params - Parameters forwarded to `authClient.getSession`.
|
|
99
|
+
*/
|
|
100
|
+
export const fetchSession = <TAuthClient extends AuthClient>(
|
|
101
|
+
queryClient: QueryClient,
|
|
102
|
+
authClient: TAuthClient,
|
|
103
|
+
params?: SessionParams<TAuthClient>
|
|
104
|
+
) => queryClient.fetchQuery(sessionOptions(authClient, params))
|
|
105
|
+
|
|
106
|
+
export type UseSessionOptions<TAuthClient extends AuthClient> =
|
|
107
|
+
SessionOptions<TAuthClient> & SessionParams<TAuthClient>
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Subscribe to the current session via TanStack Query.
|
|
111
|
+
*
|
|
112
|
+
* Shares a query key with the server-side `sessionOptions`, so SSR-hydrated
|
|
113
|
+
* session data is reused from the cache without an immediate refetch.
|
|
114
|
+
*
|
|
115
|
+
* @param authClient - The Better Auth client.
|
|
116
|
+
* @param options - `getSession` params (`query`, `fetchOptions`) merged
|
|
117
|
+
* with `useQuery` options (e.g. `enabled`, `staleTime`, `select`).
|
|
118
|
+
* @param queryClient - Optional custom `QueryClient`. Defaults to the client
|
|
119
|
+
* from the nearest `QueryClientProvider`.
|
|
120
|
+
*/
|
|
121
|
+
export function useSession<TAuthClient extends AuthClient>(
|
|
122
|
+
authClient: TAuthClient,
|
|
123
|
+
options: UseSessionOptions<TAuthClient> = {},
|
|
124
|
+
queryClient?: QueryClient
|
|
125
|
+
) {
|
|
126
|
+
const { query, fetchOptions, ...queryOptions } = options
|
|
127
|
+
|
|
128
|
+
return useQuery(
|
|
129
|
+
{
|
|
130
|
+
...sessionOptions(authClient, { query, fetchOptions }),
|
|
131
|
+
...queryOptions
|
|
132
|
+
},
|
|
133
|
+
queryClient
|
|
134
|
+
)
|
|
135
|
+
}
|
|
@@ -1,34 +1,83 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
type DataTag,
|
|
3
|
+
type QueryKey,
|
|
4
|
+
queryOptions,
|
|
5
|
+
type UseQueryOptions
|
|
6
|
+
} from "@tanstack/react-query"
|
|
2
7
|
import type { BetterFetchError, BetterFetchOption } from "better-auth/client"
|
|
3
8
|
|
|
4
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Read-style Better Auth client method (params shape `{ query?, fetchOptions? }`).
|
|
11
|
+
* Mutation-style endpoints use `AuthMutationFn` / `useAuthMutation` instead.
|
|
12
|
+
*/
|
|
13
|
+
export type AuthQueryFn<TData = unknown> = (params: {
|
|
5
14
|
query?: Record<string, unknown>
|
|
6
15
|
fetchOptions?: BetterFetchOption
|
|
7
16
|
}) => Promise<{ data: TData }>
|
|
8
17
|
|
|
9
|
-
type
|
|
18
|
+
export type AuthQueryFnData<TFn> =
|
|
19
|
+
TFn extends AuthQueryFn<infer TData> ? TData : never
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Final query key produced by {@link authQueryOptions}: the caller's prefix
|
|
23
|
+
* followed by `params.query ?? null`.
|
|
24
|
+
*/
|
|
25
|
+
export type AuthQueryKey<
|
|
26
|
+
TFn extends AuthQueryFn = AuthQueryFn,
|
|
27
|
+
TPrefix extends QueryKey = QueryKey
|
|
28
|
+
> = readonly [...TPrefix, NonNullable<Parameters<TFn>[0]>["query"] | null]
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Return type of {@link authQueryOptions}. Named so TypeScript emits
|
|
32
|
+
* `DataTag<…>` by reference in the `.d.ts`, instead of the raw
|
|
33
|
+
* `{ [dataTagSymbol]: … }` intersection — which triggers a declaration-emit
|
|
34
|
+
* bug where `dataTagSymbol` isn't re-imported at the use site and silently
|
|
35
|
+
* breaks `setQueryData`/`getQueryData` type inference at the consumer.
|
|
36
|
+
*/
|
|
37
|
+
export type AuthQueryOptions<
|
|
38
|
+
TFn extends AuthQueryFn = AuthQueryFn,
|
|
39
|
+
TPrefix extends QueryKey = QueryKey
|
|
40
|
+
> = Omit<
|
|
41
|
+
UseQueryOptions<
|
|
42
|
+
AuthQueryFnData<TFn>,
|
|
43
|
+
BetterFetchError,
|
|
44
|
+
AuthQueryFnData<TFn>,
|
|
45
|
+
AuthQueryKey<TFn, TPrefix>
|
|
46
|
+
>,
|
|
47
|
+
"queryKey"
|
|
48
|
+
> & {
|
|
49
|
+
queryKey: DataTag<
|
|
50
|
+
AuthQueryKey<TFn, TPrefix>,
|
|
51
|
+
AuthQueryFnData<TFn>,
|
|
52
|
+
BetterFetchError
|
|
53
|
+
>
|
|
54
|
+
}
|
|
10
55
|
|
|
11
56
|
/**
|
|
12
57
|
* Build `queryOptions` for a Better Auth endpoint.
|
|
13
58
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* `
|
|
59
|
+
* Injects React Query's `signal` and `throw: true` into `fetchOptions` so the
|
|
60
|
+
* request is cancelled on unmount and errors surface as `BetterFetchError`
|
|
61
|
+
* instead of `{ data, error }`.
|
|
17
62
|
*
|
|
18
63
|
* @param authFn - Better Auth client method (e.g. `authClient.getSession`).
|
|
19
64
|
* @param queryKey - Scope prefix for the key. `params.query` is appended automatically.
|
|
20
65
|
* @param params - Parameters forwarded to `authFn`.
|
|
21
66
|
*/
|
|
22
67
|
export function authQueryOptions<
|
|
23
|
-
TFn extends
|
|
24
|
-
const
|
|
25
|
-
>(
|
|
26
|
-
|
|
68
|
+
TFn extends AuthQueryFn,
|
|
69
|
+
const TPrefix extends QueryKey
|
|
70
|
+
>(
|
|
71
|
+
authFn: TFn,
|
|
72
|
+
queryKey: TPrefix,
|
|
73
|
+
params?: Parameters<TFn>[0]
|
|
74
|
+
): AuthQueryOptions<TFn, TPrefix> {
|
|
75
|
+
return queryOptions<AuthQueryFnData<TFn>, BetterFetchError>({
|
|
27
76
|
queryKey: [...queryKey, params?.query ?? null] as const,
|
|
28
77
|
queryFn: ({ signal }) =>
|
|
29
78
|
authFn({
|
|
30
79
|
...params,
|
|
31
80
|
fetchOptions: { ...params?.fetchOptions, signal, throw: true }
|
|
32
|
-
}) as Promise<
|
|
33
|
-
})
|
|
81
|
+
}) as Promise<AuthQueryFnData<TFn>>
|
|
82
|
+
}) as AuthQueryOptions<TFn, TPrefix>
|
|
34
83
|
}
|