I’m using Wix’s member area for my site, so users can log in and have certain pages that only logged-in members can view.
On one of these pages, I want to display “Welcome back, ” where is the logged-in user’s first name. I’ve been able to retrieve the current user’s first name from the members database, however, when another user logs in at the same time, the text sometimes displays their name to a different user. E.g., if myself and a friend were logged in at the same time, I might see their name, even though I’m logged in as me.
I can understand why this would happen based on the code, but I don’t really know what to do to fix it. I just want to display a logged-in user’s own name to them.
Here’s the code I have:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
let user = wixUsers.currentUser;
let userId = user.id;
wixData.query("Members/PrivateMembersData")
.eq("_id", userId)
.find()
.then( (results) => {
$w("#userName").text = results.items[0].firstName;
})
});
It definitely should never happen.
Let me understand - you logged in from your computer using your account and another member logged-in from another computer using his/her own account (with different email and credentials) and yet you saw his/her details?
Yes, I was logged in (via Google, not that it should make much difference) and he was also logged in via Google. When he was logged in at the same time as me, I could sometimes see his name, and sometimes my name.
Hey @alexgarton you are using both dataset and code on your site to retrieve the user name.
This causes the bug. You can delete the dataset and use only the code.
It would be best if you also validated that the user is logged-in before running the code; otherwise, the code fails.
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
const user = wixUsers.currentUser;
const userId = user.id;
const loggedIn = user.loggedIn;
if (loggedIn) {
wixData.query("Members/PrivateMembersData")
.eq("_id", userId)
.find()
.then((results) => {
$w("#userName").text = results.items[0].firstName;
})
}
});
Regarding you saw your last logged in friend’s name.
It’s happening because you are an admin of the site, so you have permission to see all member’s data.
You saw the last member logged in because the dataset retrieves first the last edited member, who is your friend.