[Code] Block Access After x Seconds

Article author
Duncan Hamra

In this article, Nicolas breaks down two custom code snippets you can use to block access to content after a certain number of seconds have passed. After reaching the number-of-seconds-on-page threshold, the user will get kicked to a page of your choosing. 

Block Access from Non-Members

In this article, Nicolas breaks down a custom code snippet you can use to block access to content for non-members. In other words, all members will have access and will not get redirected.

You'll want to add this code to the Footer or <Body> section of any page that appears in your list of pages (0:33 in the video above). 


<script>
function redirectAfterSeconds({ seconds, pages, redirectTo }) {
  if (typeof seconds !== "number") {
    throw new Error("seconds must be a number");
  }
  if (pages === "*" && window.location.pathname !== redirectTo) {
    setTimeout(() => {
      window.location.href = redirectTo;
    }, seconds * 1000);
  } else if (Array.isArray(pages) && pages.includes(window.location.pathname)) {
    setTimeout(() => {
      window.location.href = redirectTo;
    }, seconds * 1000);
  }
}

window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
  !member && redirectAfterSeconds({ 
	  seconds: 5, 
	  pages: "*", // defaults to all pages or you can pass an array of pages ["/page, "/page"]
	  redirectTo: "/access-denied" 
  });
});
</script>

 

Block Access Based on Plans

In this section, Nicolas will show you how to block access to content based on a member's plans.

Copy and paste ONE of the following code snippets into your site's Footer or <Body> section of your site. The first will redirect members who DO have a plan in the list. The 2nd will redirect member's who DO NOT have a plan. 

Redirect If Member HAS a Plan

<script>
const PLANS_TO_TRACK = ["pln_cl0vdsu0w000308jnh6lkd7bn", "pln_cku4gsei0000209l2hj3fdb4l"]

function redirectAfterSeconds({ seconds, pages, redirectTo }) {
  if (typeof seconds !== "number") {
    throw new Error("seconds must be a number");
  }
  if (pages === "*" && window.location.pathname !== redirectTo) {
    setTimeout(() => {
      window.location.href = redirectTo;
    }, seconds * 1000);
  } else if (Array.isArray(pages) && pages.includes(window.location.pathname)) {
    setTimeout(() => {
      window.location.href = redirectTo;
    }, seconds * 1000);
  }
}
function checkForPlans({ plans, filter }) {
  return plans.some(plan => {
    let hasPlan = filter.includes(plan.planId) && plan.status === "ACTIVE"
    return hasPlan
  });
}

window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
	let plans = member?.["planConnections"]
	if(member && plans) {
	  let hasPlans = checkForPlans({ plans, filter: PLANS_TO_TRACK })
	  hasPlans && redirectAfterSeconds({ 
        seconds: 5, 
        pages: "*", // defaults to all pages 
        // but you can pass an array of multiple pages ["/page, "/page"]
        redirectTo: "/access-denied" 
      });
	}
});
</script>

Redirect if Member Does NOT Have a Plan

<script>
const PLANS_TO_TRACK = ["pln_cl0vdsu0w000308jnh6lkd7bn", "pln_cku4gsei0000209l2hj3fdb4l"]

function redirectAfterSeconds({ seconds, pages, redirectTo }) {
  if (typeof seconds !== "number") {
    throw new Error("seconds must be a number");
  }
  if (pages === "*" && window.location.pathname !== redirectTo) {
    setTimeout(() => {
      window.location.href = redirectTo;
    }, seconds * 1000);
  } else if (Array.isArray(pages) && pages.includes(window.location.pathname)) {
    setTimeout(() => {
      window.location.href = redirectTo;
    }, seconds * 1000);
  }
}
function checkForPlans({ plans, filter }) {
  return plans.some(plan => {
    let hasPlan = filter.includes(plan.planId) && plan.status === "ACTIVE"
    return hasPlan
  });
}

window.$memberstackDom.getCurrentMember().then(({ data: member }) => {
	let plans = member?.["planConnections"]
	if(member && plans) {
	  let hasPlans = checkForPlans({ plans, filter: PLANS_TO_TRACK })
	  !hasPlans && redirectAfterSeconds({ 
        seconds: 5, 
        pages: "*", // defaults to all pages 
        // but you can pass an array of multiple pages ["/page, "/page"]
        redirectTo: "/access-denied" 
      });
	}
});
</script>

Was this article helpful?

Comments

2 comments

  • Comment author
    William de Broucker

    Great ! I guess we could combine that with a condition on the membership of the user

    1
  • Comment author
    Duncan Hamra

    Yes indeed! Nicolas Scott actually mentioned to me that he'd be working on scripts to handle plans in the near future. 

    1

Please sign in to leave a comment.