How to let a user log in a specific page

Hi,.
I have two database collections: Customer and Suppliers, everyone with their own page.
1. The Customer user only can acces in Customer page and not in Suppliers page.
2. The Supplier user only can acces in Supplier page and not in Customer page.

Previously i added a Customer or Supplier with:
const toInsert = {
“_id”: userId,
“email”: userEmail
};
// add the item to the collection
wixData.insert(“Customer”, toInsert) // or “Supplier”, toInsert…
. catch ( (err) => {
console.log(err);
} );

Then, to validate the log in, my code is:
// for Customer page:

let userID = wixUsers.currentUser.id; // to get the user id.
//If userid exists in customer collection, open the page Customer.
wixData.query(“customer”)
.eq(“_id”, userID)
.find()
.then( (results) => {
wixLocation.to(“/login-customer”);
}
// else show a message.
$w(“#text1”).text = “Acces denied! You are not a Customer!”;

If a Supplier logged in in his own page, he can see the customer page like too and vice versa.
The message in $w(“#text1”).text never shows.

There will be an error in my code?

hi,

The find() function returns a promise also if no result was found.
Therefore, use it as follows:

//…
.find()
.then((results) => {
if (results.length > 0) {
//found a result, user is a customer
wixLocation.to(“/login-customer”);
}
else {
$w(“#text1”).text = “Acces denied! You are not a Customer!”;
}
}

//…