Member Document Access

Question:
How can I ensure that our site members have exclusive access to certain collections based on their email address or a specific badge assignment?

Product:
Wix Editor

What are you trying to achieve:
MEMBER1 should show content that’s relevant only to them when they click on My Documents.

What have you already tried:

  1. MEMBER#1 has a badge assigned to them called “7499” (each member will have their own badge exclusive to them)
  2. CRM Collection created: “MyDocuments” with text fields to collect badgeName and userEmail. This is where documents will be uploaded for members with their unique badgename
  3. CRM Collection created: “userProfiles” with text fields to record badgeName and userEmail (this is so Wix can match the email to the badge)
  4. Dynamic page create for “My Documents” linking it back to the MyDocuments collection. No filter set due to over-ride by below code
  5. Code entered into Developer Mode

Additional information:
If there is a way to do this without inserting code, I would like to go that route. But, here is the code entered into Developer Mode:
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import {session} from ‘wix-storage’;

$w.onReady(function () {
// Check if a user is logged in
if (wixUsers.currentUser.loggedIn) {
// Retrieve the logged-in user’s email
wixUsers.currentUser.getEmail().then((email) => {
// Query the ‘UserProfiles’ collection to find the user’s profile by their email
wixData.query(‘UserProfiles’)
.eq(‘userEmail’, email)
.find()
.then((results) => {
if (results.items.length > 0) {
// Assuming the user’s profile was found, extract the badgeName
let userBadgeName = results.items[0].badgeName;

        // Ensure the 'MyDocuments' dataset is ready before applying the filter
        $w("#MyDocuments").onReady(() => {
          // Apply a filter to the 'MyDocuments' dataset to only show documents with the user's badgeName
          $w("#MyDocuments").setFilter(wixData.filter()
            .eq('badgeName', userBadgeName)
          );
        });
      } else {
        console.log("No user profile found.");
      }
    })
    .catch((err) => {
      console.error("Error querying UserProfiles:", err);
    });
});

} else {
// User is not logged in, prompt them to log in
let currentPage = wixLocation.url; // Store the current page URL
session.setItem(“returnToPage”, currentPage); // Save the current page to return to after login

// Redirect to the login page or open a login lightbox
wixUsers.promptLogin({ mode: "login" })
  .then(() => {
    // User is now logged in, redirect them back to the page they were trying to access
    let returnToPage = session.getItem("returnToPage");
    if (returnToPage) {
      wixLocation.to(returnToPage); // Redirect user back to the original page
    } else {
      // If the page to return to isn't specified, redirect to a default page or refresh
      wixLocation.to("/"); // Adjust this to your preferred default page
    }
  })
  .catch((err) => {
    console.error("Error during login:", err);
  });

}
});

There are simpler ways, yes. You will still need a little code, and a different logic flow. But in the end you will have a better user experience (and it will be easier for you to maintain).

You don’t have to use a dynamic page if the dynamic page is meant for a single person only. You can create a Members Area private page and filter it to show matching items for that single person only.

The Member area page already requires a person to be logged in so they can access the page, so that saves you on that code. It also saves you on the redirect code to send them to a specific dynamic page. With the private Members Area page you can have a simple URL ending for everyone: .com/members-area/my-documents

If you are only creating a badge number to simply filter data later, you don’t have to. Otherwise you will end up with 1,000 badges for a 1,000 different people. If this badge number is used externally or for other reasons, then you can continue using this method. Just know that you don’t have to use the badge number as the “field you have to filter by”.

This will help you on the logic of the filtering:

#codequeen

Thanks for the reply. Let me clarify a little more. Every member will have it’s own unique badge name. In the Collections, there are documents uploaded with a badge name associated with it. In my mind, the member with a badge name of 1234, should only see documents with that same badge name associated with it. The issue I’m having is with the filters. The options to filter are “Equal to” “Not Equal to” “Less Than” and etc.

The easy option I was hoping or is that documents can be uploaded somewhere for a member and that member can view those documents from the member page.

If you are trying to filters without code then you must be using a number field instead of a text field.

Change the field type.

But also, consider the logic flow that I shared with you earlier.