UNPKG

@dynamic-labs/sdk-react-core

Version:

A React SDK for implementing wallet web3 authentication and authorization to your website.

47 lines (44 loc) 2.21 kB
'use client' import { __awaiter } from '../../../../../_virtual/_tslib.js'; import { useCallback } from 'react'; import '@dynamic-labs/utils'; import '@dynamic-labs/sdk-api-core'; import '../../../config/ApiEndpoint.js'; import '../../../store/state/projectSettings/projectSettings.js'; import '../../constants/values.js'; import '@dynamic-labs/multi-wallet'; import '../../../shared/logger.js'; import '../../constants/colors.js'; import 'react-international-phone'; import '@dynamic-labs/iconic'; import '@dynamic-labs/wallet-connector-core'; import 'react/jsx-runtime'; import '../../../context/ViewContext/ViewContext.js'; import '@dynamic-labs/wallet-book'; import '../../../shared/consts/index.js'; import '../../../store/state/nonce/nonce.js'; import { getEnvironmentId } from '../../../store/state/dynamicContextProps/dynamicContextProps.js'; import { getPrimaryWalletId, setPrimaryWalletId } from '../../../store/state/primaryWalletId/primaryWalletId.js'; import '../../../store/state/user/user.js'; import '../../../store/state/connectedWalletsInfo/connectedWalletsInfo.js'; import '../../../locale/locale.js'; import { refreshUserJwt } from '../../../data/api/user/user.js'; /** * Refresh user state * @returns A callback function that can be used to refresh the user state */ const useRefreshUser = () => useCallback(() => __awaiter(void 0, void 0, void 0, function* () { var _a; const user = yield refreshUserJwt({ environmentId: getEnvironmentId() }); if (!(user === null || user === void 0 ? void 0 : user.verifiedCredentials)) throw new Error('useRefreshUser resulted in an invalid user: no verified credentials found'); // Check if the primary wallet is still in this user's VCs const primaryWalletInUserVCs = user.verifiedCredentials.some(({ id }) => id === getPrimaryWalletId()); if (primaryWalletInUserVCs) return user; // We need to switch primary wallet to the first wallet in the user's VCs const newPrimaryWalletId = (_a = user.verifiedCredentials.find(({ format }) => format === 'blockchain')) === null || _a === void 0 ? void 0 : _a.id; setPrimaryWalletId(newPrimaryWalletId); return user; }), []); export { useRefreshUser };