Membership

Methods for retrieving corporate or group membership access based on a user's IP address or email domain.

getByIP

Get an array of memberships associated with the user's IP address.

💡

Client-Side Caching
To enhance performance and reduce the need for repeated API requests, this method caches API responses in the browser's session storage. Please be aware of the following:

  • What's Cached: Both successful (200) and "not found" (404) responses.
  • Cache Duration: Responses are cached for 5 hours.
  • Storage Key: pelcro.membership.forThisIP.
  • Handling 404: An error is always sent to your callback (even for cached 404s), and the data object will be null.

Example

/**
 * Callback function for Pelcro.membership.getByIP().
 *
 * @param {import('axios').AxiosError|null} error - An AxiosError object if the request failed, or null if successful.
 * @param {Array|{data: Array}|null} memberships - The memberships array or an object containing the memberships array in the 'data' property, or null if an error occurred.
 *
 * @returns {any} - Depends on your implementation.
*/
const handleGetMembershipByIPCallback = (error, memberships) => {
  if (error) {
    if (error.response?.status === 404) {
      console.debug("No memberships found for this IP (404)");
      // Handle the "no memberships" case (e.g., display a message, toggle paywall, ...)
      return null; // Or return a specific value indicating no data.
    } else {
      console.error("Error fetching memberships:", error.message);
      // Handle other errors (e.g., display an error message, trigger fallback mechanism logic, ...)
      return null; // Or return an appropriate error value if needed.
    }
  }
  
  // Handle the case where memberships is an object.
  if (memberships && !Array.isArray(memberships) && memberships.data) {
    memberships = memberships.data;
  }
  
  console.debug("IP has matching memberships:", memberships);
  return memberships;
};

window.Pelcro.membership.getByIP(handleGetMembershipByIPCallback);

getByEmail

Get an array of memberships associated with the user email address's domain.

Example

window.Pelcro.membership.getByEmail(
    { 
      email: "[email protected]" 
    },
    (error, resp) => {
			if (error) {
  			return console.log("error", error.message);
      }
      
  		const memberships = resp;
  		console.log(memberships);          
});