UNPKG

@churchapps/apphelper

Version:

Library of helper functions for React and NextJS ChurchApps

97 lines 3.64 kB
var _a; import { ApiHelper } from "@churchapps/helpers"; export class UserHelper { static setupApiHelper(userChurch) { ApiHelper.setDefaultPermissions(userChurch.jwt); userChurch.apis.forEach(api => { ApiHelper.setPermissions(api.keyName, api.jwt, api.permissions); }); } static setupApiHelperNoChurch(user) { ApiHelper.setDefaultPermissions(user.jwt); } static checkAccess({ api, contentType, action }) { const config = ApiHelper.getConfig(api); if (!config) return false; const permissions = config.permissions || []; let result = false; if (permissions !== undefined) { permissions.forEach((element) => { if (element.contentType === contentType && element.action === action) result = true; }); } return result; } static createAppUrl(appUrl, returnUrl) { const jwt = ApiHelper.getConfig("MembershipApi")?.jwt; if (jwt) { return `${appUrl}/login/?jwt=${jwt}&returnUrl=${encodeURIComponent(returnUrl)}`; } else { return `${appUrl}/login/?returnUrl=${encodeURIComponent(returnUrl)}`; } } static redirectToLogin(returnUrl, handleRedirect) { if (typeof window !== "undefined") { const currentUrl = returnUrl || window.location.pathname + window.location.search; const encodedReturnUrl = encodeURIComponent(currentUrl); const loginUrl = `/login?returnUrl=${encodedReturnUrl}`; // Use handleRedirect function if available, otherwise fallback to window.location if (handleRedirect) { handleRedirect(loginUrl); } else { window.location.href = loginUrl; } } } } _a = UserHelper; UserHelper.churchChanged = false; UserHelper.selectChurch = async (context, churchId, keyName) => { let userChurch = null; // Ensure userChurches is initialized if (!_a.userChurches || !Array.isArray(_a.userChurches)) { console.error("UserHelper.userChurches is not initialized or not an array:", _a.userChurches); // Try to get userChurches from context if available if (context?.userChurches && Array.isArray(context.userChurches)) { _a.userChurches = context.userChurches; } else { console.error("Cannot select church: no valid userChurches available"); return; } } if (churchId) { _a.userChurches.forEach(uc => { if (uc.church.id === churchId) userChurch = uc; }); } else if (keyName) _a.userChurches.forEach(uc => { if (uc.church.subDomain === keyName) userChurch = uc; }); else userChurch = _a.userChurches[0]; if (!userChurch) return; else { _a.currentUserChurch = userChurch; _a.setupApiHelper(_a.currentUserChurch); // TODO - remove context code from here and perform the logic in the component itself. if (context) { if (context.userChurch !== null) _a.churchChanged = true; context.setUserChurch(_a.currentUserChurch); // Also ensure user is still set in context if (_a.user && context.setUser) { context.setUser(_a.user); } // Update person if available if (_a.person && context.setPerson) { context.setPerson(_a.person); } } } }; //# sourceMappingURL=UserHelper.js.map