About this article

  • Difficulty level: Hard
  • Technical background needed: Advanced
  • Estimated time required to complete: 30 minutes +

What?

This article will cover three topics:

  1. Accessing member information with JS
  2. Updating member profile information with JS
  3. Updating and retrieving the member JSON object

Quick Note: The front-end API only allows 5 requests per second per IP address.

1. Accessing member information with JS

How to access member information with JS:

MemberStack.onReady.then(function(member) {   
  // do things with the member object
  member["email"]
  member["name"]
  member["id"] //current member id  

  // member's specific page if set.
  member.memberPage

  // check if member is logged in  
  member.loggedIn // returns true or false
})

You have access to any fields you collect during signup, just like you do when using ms-data.

You can also access the member's membership info.

MemberStack.onReady.then(function(member) {   
    var membership = member.membership  
    membership.id //current membership  
    membership.status //active, canceled, past_due, unpaid, trialing, or sca
    membership.current_period_end // unix timestamp
    membership.cancel_at_period_end // true or false
})

The current period end is a unix timestamp. Here's an example on how to convert it into a date object:

var date = new Date(membership.current_period_end * 1000)

2. Updating member profile information with JS

The updateProfile method accepts two arguments. The first argument is an object containing the data you would like to update. The keys must match your form field values given in your Memberstack dashboard. The second argument is a boolean value. If set to true, Memberstack will show a loader and default success/error messages. If false, Memberstack will silently update the profile with no loaders or success/error messages.

MemberStack.onReady.then(function(member) {   
    member.updateProfile({
        "first-name": "Dave",
        "last-name": "Mathews"  
    }, false)
})

You can use then and catch to know when the update is done and to handle errors.

3. Updating and retrieving a member's JSON object

Memberstack allows you to extend its functionality through a member JSON object. You have the ability to update and access each member's JSON Object.

Important: The data you send must be valid JSON.

Updating:

To set/update a JSON object, use the updateMetaData member function.

MemberStack.onReady.then(function(member) { 
    var memberProgress = {
        lesson1: "finished",
        lesson2: "in-progress"
    }
    member.updateMetaData(memberProgress)
})

You can update a single property without affecting others.

lesson1 data will not be changed.

MemberStack.onReady.then(function(member) { 
    var memberProgress = {
        lesson2: "finished"
    }
    member.updateMetaData(memberProgress)
})

You can remove properties by setting their value to null.

Lesson1 and lesson2 data will be removed.

MemberStack.onReady.then(function(member) {
    var memberProgress = {
        lesson1: null,
        lesson2: null
    }
    member.updateMetaData(memberProgress)
})

Retrieving:

MemberStack.onReady.then(async function(member) {
    var metadata = await member.getMetaData()
    // do stuff with members data
})

Keywords: API, front-end, member info, JSON object, JS

Did this answer your question?