Skip to main content

Member Management with the Memberstack DOM Package

Josh Lopez
Josh Lopez

Member management is a critical part of any application with registered users. The Memberstack DOM (Document Object Model) package provides a robust set of methods to help you manage members directly within your JavaScript code. With the DOM package, you can fetch member profiles, update member details, manage access to different parts of your app, and more. This empowers you to build customized experiences for members without needing to build out complex user infrastructure from scratch.

View the full DOM Package documentation for more guidance.

This guide will walk you through the most common member management tasks utilizing the methods available in the Memberstack DOM package. We'll cover fetching member data, updating member details, working with member JSON, and managing access to free plans.

Note: The Memberstack DOM Package can be accessed through the Webflow Package as well by using window.$memberstackDom.getCurrentMember() instead of memberstack.getCurrentMember().

1) Fetching Member Data

Once a member is logged in, you can retrieve information about them from Memberstack.

Get Currently Logged In Member

getCurrentMember() resolves to { data: member }. When nobody is logged in, data is null (it does not throw), so check it before using it:

// Get the currently logged in member
const { data: member } = await memberstack.getCurrentMember();

if (member) {
  // Access member properties
  console.log(member.id);
  console.log(member.auth.email);
  console.log(member.customFields);
} else {
  // Nobody is logged in
  console.log('Not logged in');
}

The member object contains the member's ID, email (member.auth.email), whether their email is verified (member.verified), custom fields, metadata, plan connections, and more.

The other methods in this article need a logged-in member. If nobody is logged in they reject: addPlan() with the code login-required, and the others with the plain string "Unauthorized" rather than an error object with a code. Check for a member first.

2) Updating Member Details

You may want to enable members to update their own profile information and settings. Here are some useful methods for updates:

Update Custom Fields

Custom fields allow you to store structured data on members. You can update them with:

// Update the current member's custom fields.
// 'country' must be the key of a custom field in your app.
const { data: member } = await memberstack.updateMember({
  customFields: {
    country: 'Canada'
  }
});

console.log(member.customFields.country); // 'Canada'

This sets the member's country value to 'Canada' and leaves their other custom fields unchanged. It resolves to { data: member } with the updated member.

Only keys of custom fields that exist in your app are saved. Any other key, and any custom field that is restricted to admins, is ignored without an error. updateMember() only changes custom fields; if you pass metaData it is ignored.

Change Member's Email

// New email address
const newEmail = 'newemail@example.com';

// Update current member's email
const { data: member } = await memberstack.updateMemberAuth({
  email: newEmail
});

console.log(member.auth.email); // 'newemail@example.com'
console.log(member.verified);   // false

This will update the member's email in Memberstack. The new email must be a valid address that no member of your app already uses, including this member (resubmitting their current email also fails); otherwise the call rejects with the code invalid-email or email-already-in-use. Emails are saved in lowercase. Changing the email sets member.verified to false, and if your app has email verification turned on, Memberstack sends a verification email to the new address.

Update Current Member Password

Allow members to change their password with:

// Current password
const currentPassword = 'existing_password';

// New password (at least 8 characters)
const newPassword = 'new_secure_password';

try {
  // Update password
  await memberstack.updateMemberAuth({
    oldPassword: currentPassword,
    newPassword: newPassword
  });
} catch (error) {
  // For example 'invalid-password' (wrong current password)
  // or 'invalid-password-too-short'
  console.log(error.code, error.message);
}

This will update the member's password in Memberstack, and the member stays logged in. Both oldPassword and newPassword are required. Passing only one of them rejects with the code generic-message and the message "No valid parameters provided for update"; if you also pass email, only the email is changed and the lone password field is ignored.

Send Member Verification Email

Prompt Memberstack to re-send the member's verification email:

await memberstack.sendMemberVerificationEmail();

This sends a verification email to the logged-in member's current email address and resolves to { data: { success: true } }. If one was already sent in the last minute, it resolves the same way without sending another. If the email can't be sent, it rejects with the code email-verification-send-failed.

3) Working with Member JSON Data

The member JSON attribute allows you to store custom profile data for each member.

Get Member JSON

Retrieve the member's JSON data. getMemberJSON() resolves to { data }, where data is the stored JSON, or null if nothing has been saved yet:

// Get member JSON (data is null until something has been saved)
const { data } = await memberstack.getMemberJSON();
const json = data || {};

// Access data
console.log(json.userName);
console.log(json.avatarURL);

Update Member JSON

updateMemberJSON() replaces the member's whole JSON with the object you pass; it doesn't merge. To change part of it, first get the current data, modify it, then save it:

// Get current JSON
const { data } = await memberstack.getMemberJSON();
const json = data || {};

// Add/update properties
json.avatarURL = 'new_avatar_url';

// Remove a property
delete json.oldProperty;

// Save it: this replaces the member's whole JSON with this object
await memberstack.updateMemberJSON({ json });

Because you save the object you read, the rest of the member's JSON is preserved.

4) Managing Access

You can also manage member access to different parts of your application with free plans. Copy a plan's ID from the plan in the Plans section of your dashboard.

Add Free Plan Access

Grant access to a part of your app by adding a free plan:

// ID of the free plan to grant access to
const planId = 'pln_your-free-plan-id';

// Add free plan to current member
const { data } = await memberstack.addPlan({
  planId
});

console.log(data.member.planConnections);

This will connect the member to the free plan, granting access. addPlan() only works with free plans: a plan with prices rejects with the code plan-not-free, a plan the member already has with already-have-plan, and an unknown or archived plan ID with plan-not-found. A free plan can also reject with plan-restricted-to-admin, domain-not-permitted or plan-member-limit-reached, depending on the plan's settings.

Revoke Free Plan Access

You can revoke access by removing a free plan:

// ID of the free plan to revoke access from
const planId = 'pln_your-free-plan-id';

// Remove free plan from current member
const { data } = await memberstack.removePlan({
  planId
});

console.log(data.member.planConnections);

This disconnects the member from the plan, revoking access. removePlan() only removes free plans: a paid plan's ID rejects with the code plan-not-found, and a plan the member doesn't have with no-plan-found.

Check out the other articles in this series:

  1. Getting Started with the Memberstack DOM Package (Front-end API)
  2. Working with Memberstack DOM Package Modals
  3. Managing App Data with the Memberstack DOM Package
  4. Member Authentication with the Memberstack DOM Package
  5. Member Billing and Checkout with the Memberstack DOM Package
  6. Member Management with the Memberstack DOM Package
  7. Events and Webhooks with Memberstack DOM

Was this article helpful?

Didn’t find your answer?

Get an instant answer from Rey, or reach a human. Either way, we’re happy to help.

Comments

2 comments

  • Joao Filipe
    Joao Filipe

    Can I update user custom fields via frontend with await memberstack.updateMember or do I need to create a hidden profile form that gets submitted upon some action?

    0
  • Michael Wells
    Michael Wells

    You can update directly from your JS. 

    0

Please sign in to leave a comment.