I have recently created a website on Wix that requires you to log in to access the “Client Portal” member page I created. The client portal page is essentially a database (collection) in table form that allows users to select and view documents pertaining to their company. The ClientPortal2 collection has 3 fields: the name of the document, the actual document, and the company it is affiliated with. The Client Portal database is shown below. I want to restrict users to only be able to see the documents that are affiliated with their company so I made another collection called Users2. Users2 references all of the users that have an account in a multi reference field (email) and the company that they work for in another field (company). Users2 is also shown below. In summary, I want users to ONLY be able to look at their company’s documents and no one else’s.
CLIENTPORTAL2
Users2
The code I wrote to sort the documents based on the certain user’s affiliated company is as shown below:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
let user = wixUsers.currentUser;
user.getEmail()
.then( (currentEmail) => {
let userEmail = currentEmail; // "user@something.com"
});
user.getEmail()
.then( (currentEmail) => {
wixData.query('Users2').include('email')
.find().then((results) => {
for (var i=0;i<results.items.length ;i++) {
for (var j=0;j<results.items[i].email.length ;j++) {
let userLoginEmail = results.items[i].email[j].loginEmail;
if (userLoginEmail === currentEmail){
let userCompany = results.items[i].company
filter(userCompany);
}
}
}
})
})
});
function filter(Company){
wixData.query('CLIENTPORTAL2').eq("company", Company)
.find().then( (results) => {
let tableData = results.items;
$w('#table1').rows = tableData;
console.log(results.items);
console.log(Company);
})
}
The code runs a query on ‘Users2’ and looks at all of the users’ email addresses and compares it to the current logged in user’s address. It then runs a query on ‘ClientPortal2’ using the users affiliated company it just found in the first query. The table (#table1) displayed on the Client Portal page now displays the documents pertaining to the company the user works for. So for example, if ‘dew0025@auburn.edu’ was logged into the site, that user would only be able to view the documents in the table below because that user is restricted to ‘Company 1’
#table1 displayed on Client Portal Members Page
So this code works well but it only works on the preview page and not the live published page. What am I doing wrong? Is there an easier way of doing this or am I going about this the wrong way? Thanks in advance.