UNPKG

@hhgtech/hhg-components

Version:
99 lines (92 loc) 4.82 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var React = require('react'); var miscCookieHelper = require('./miscCookieHelper.js'); var store = require('./store-c7185440.js'); var togetherComponentGlobalContext = require('./utils-aea77f4a.js'); var constants = require('./constants-bb30dda6.js'); require('./tslib.es6-92cccef3.js'); require('zustand'); require('./index-fa7abbff.js'); require('./constantsIsProduction.js'); require('./normalizeLink-4fe5440a.js'); require('./constantsDomainLocales.js'); require('./Locale-59ccf941.js'); require('./index-1ee4ebb8.js'); require('date-fns/locale'); require('./index-ad7155cf.js'); require('./constantsSite.js'); require('./constantsRiskScreener.js'); require('./index-3020fcb3.js'); require('slugify'); require('string-format'); require('./togetherApiPaths.js'); function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefault(React); const TogetherAuthConfigContext = React.createContext({}); const TogetherAuthConfigContextProvider = ({ children, defaultParams, }) => { return (React__default["default"].createElement(TogetherAuthConfigContext.Provider, { value: { defaultParams, } }, children)); }; const useTogetherAuthRequiredAction = (propsParams) => { const { triggerLogin } = store.useSSOV2Store(); const { data: { userInfo, env: { togetherBasePath }, }, } = React.useContext(togetherComponentGlobalContext.TogetherComponentGlobalContext); const { defaultParams } = React.useContext(TogetherAuthConfigContext) || {}; const loggedInRef = React.useRef(!!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id)); loggedInRef.current = !!(userInfo === null || userInfo === void 0 ? void 0 : userInfo.id); const isFirstInteractRef = React.useRef(userInfo && typeof userInfo.communities_interacted === 'number' && userInfo.communities_interacted === 0); isFirstInteractRef.current = userInfo && typeof userInfo.communities_interacted === 'number' && userInfo.communities_interacted === 0; const authActionWrapper = (func, extraParams) => { const params = Object.assign(Object.assign(Object.assign({}, defaultParams), propsParams), extraParams); if (!loggedInRef.current || isFirstInteractRef.current) { if (!loggedInRef.current) { miscCookieHelper.deleteCookie(constants.BEARER_TOKEN_COOKIE); } if (params === null || params === void 0 ? void 0 : params._joinCommunityIds) { localStorage.setItem('to_join_community_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_community_ids') || '[]') || []) .concat(params._joinCommunityIds) .filter(Boolean))))); params === null || params === void 0 ? true : delete params._joinCommunityIds; } if (params === null || params === void 0 ? void 0 : params._joinTopicIds) { localStorage.setItem('to_join_topic_ids', JSON.stringify(Array.from(new Set((JSON.parse(localStorage.getItem('to_join_topic_ids') || '[]') || []) .concat(params._joinTopicIds) .filter(Boolean))))); params === null || params === void 0 ? true : delete params._joinTopicIds; } const currentLocation = (params === null || params === void 0 ? void 0 : params._location) || window.location.href; params === null || params === void 0 ? true : delete params._location; const redirectUrl = togetherComponentGlobalContext.getUrlToSignUp(currentLocation, params, togetherBasePath); if (store.IS_SSOV2_ENABLED()) { const returnUrlParam = redirectUrl.slice(redirectUrl.indexOf('returnUrl=') + 10); if (returnUrlParam) { const originalRedirectUrl = decodeURIComponent(returnUrlParam); triggerLogin({ returnUrl: originalRedirectUrl, source: 'communities', }); } } else { window.location.href = redirectUrl; } return null; } else { return func && func(); } }; return { authActionWrapper, params: Object.assign(Object.assign({}, defaultParams), propsParams), }; }; exports.TogetherAuthConfigContext = TogetherAuthConfigContext; exports.TogetherAuthConfigContextProvider = TogetherAuthConfigContextProvider; exports.useTogetherAuthRequiredAction = useTogetherAuthRequiredAction;