UNPKG

@dynamic-labs/sdk-react-core

Version:

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

113 lines (108 loc) 5.88 kB
'use client' 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _tslib = require('../../../../../_virtual/_tslib.cjs'); var React = require('react'); var sdkApiCore = require('@dynamic-labs/sdk-api-core'); var logger = require('../../../shared/logger.cjs'); require('@dynamic-labs/iconic'); require('@dynamic-labs/wallet-connector-core'); require('react/jsx-runtime'); require('../../../context/ViewContext/ViewContext.cjs'); require('@dynamic-labs/wallet-book'); require('@dynamic-labs/utils'); require('../../constants/colors.cjs'); require('../../constants/values.cjs'); require('../../../shared/consts/index.cjs'); require('@dynamic-labs/multi-wallet'); var getAuthToken = require('../../functions/getAuthToken/getAuthToken.cjs'); require('react-international-phone'); require('../../../store/state/nonce/nonce.cjs'); var isCookieEnabled = require('../../functions/isCookieEnabled/isCookieEnabled.cjs'); var projectSettings = require('../../../store/state/projectSettings/projectSettings.cjs'); var updatePrimaryWalletId = require('../../functions/updatePrimaryWalletId/updatePrimaryWalletId.cjs'); require('../../../store/state/connectedWalletsInfo/connectedWalletsInfo.cjs'); var dynamicContextProps = require('../../../store/state/dynamicContextProps/dynamicContextProps.cjs'); require('../../../store/state/primaryWalletId/primaryWalletId.cjs'); require('../../../config/ApiEndpoint.cjs'); var user = require('../../../store/state/user/user.cjs'); require('../../../locale/locale.cjs'); var user$1 = require('../../../data/api/user/user.cjs'); var useDynamicEvents = require('../events/useDynamicEvents/useDynamicEvents.cjs'); const useRefreshUserState = () => { const [isLoading, setIsLoading] = React.useState(true); const didRefreshUserRef = React.useRef(false); const environmentId = dynamicContextProps.useEnvironmentId(); const projectSettings$1 = projectSettings.useProjectSettings(); /** * As an extra layer of safety, we must explicitly prevent the refresh from being called * after a logout */ const hasLoggedOut = React.useRef(false); useDynamicEvents.useInternalDynamicEvents('logout', () => (hasLoggedOut.current = true)); /** * run this effect every time the user or project settings change * but not when the process was already done. */ React.useEffect(() => { /** * this method is called on refresh of DynamicContext and attempts to refresh local storage * when cookies are enabled for the environment. * the use case here is when an end user logs in to sub1.example.com and navigates to sub2.example.com. * if they are signed in to sub1.example.com with a cookie for `.example.com`, but the LS is specific * to sub1, then navigating to sub2 will mean they are not automatically signed in. * this method attempts to fetch the user from the backend using the cookie (if aavailable), and if successful, * this will ensure local storage is properly set with the user. * * this will also attempt to set the primaryWalletId, if the last verified credential is a wallet * the user might have signed in with a wallet in the other subdomain, but the primary wallet * in local storage does not carry over to other subdomains (LS is subdomain-specific), * so attempt to set it here. */ const maybeInitUserRefresh = () => _tslib.__awaiter(void 0, void 0, void 0, function* () { const mustRefreshUser = (() => { // if user is present, we dont need to refresh anything if (user.getUser()) return false; // if the user has logged out, we dont need to refresh anything if (hasLoggedOut.current) return false; // should run if cookie is enabled and no user is present if (projectSettings$1 && isCookieEnabled.isCookieEnabled(projectSettings$1)) return true; // should run if auth token is present but no user is present and cookie is not enabled return Boolean(getAuthToken.getAuthToken()); })(); if (mustRefreshUser) { // Disable all next calls to this method didRefreshUserRef.current = true; try { const user = yield user$1.refreshUserJwt({ environmentId }); /** * attempt to set primary wallet in this subdomain because LS does not * carry over between different subdomains, even if the cookie does */ const lastVerifiedWallet = user === null || user === void 0 ? void 0 : user.verifiedCredentials.find((vc) => vc.format === sdkApiCore.JwtVerifiedCredentialFormatEnum.Blockchain && vc.id === user.lastVerifiedCredentialId); if (lastVerifiedWallet) { updatePrimaryWalletId.updatePrimaryWalletId(lastVerifiedWallet.id); } } catch (e) { logger.logger.debug('Ignore failed refreshUserJwt in useRefreshUserState'); } } if (projectSettings$1) { // only set to false if projectSettings are available, // otherwise isCookieEnabled might be a false negative setIsLoading(false); } }); // Ensure this effect only runs once projectSettings is available if (didRefreshUserRef.current || !projectSettings$1) return; maybeInitUserRefresh(); }, [projectSettings$1, environmentId]); return { isLoading }; }; exports.useRefreshUserState = useRefreshUserState;