How do I update this code to the new wix-members API?
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
$w.onReady(() => {
if (user.loggedIn) {
$w("#PubliLogout").collapse();
$w("#boxMemberLogoutGlobal").collapse();
$w("#boxCardMemberLoginGlobal").expand();
} else {
$w("#PubliLogout").expand();
$w("#boxMemberLogoutGlobal").expand();
$w("#boxCardMemberLoginGlobal").collapse();
}
wixUsers.onLogin(user => {
if (user.loggedIn) {
$w("#PubliLogout").collapse();
$w("#boxMemberLogoutGlobal").collapse();
$w("#boxCardMemberLoginGlobal").expand();
}
})
})
J.D
May 24, 2022, 5:08pm
2
Try:
import { currentMember, authentication} from 'wix-members';
$w.onReady(() => {
currentMember.getMember().then(member => handleUserState(member));
authentication.onLogin(member => handleUserState(member))
})
function handleUserState(member){
if (member) {
$w("#PubliLogout").collapse();
$w("#boxMemberLogoutGlobal").collapse();
$w("#boxCardMemberLoginGlobal").expand();
} else {
$w("#PubliLogout").expand();
$w("#boxMemberLogoutGlobal").expand();
$w("#boxCardMemberLoginGlobal").collapse();
}
}
J.D
May 24, 2022, 7:39pm
3
@gusc sorry, I wrote it to fast. It should be:
currentMember.getMember().then(member => handleUserState(member));
It worked as if by magic.
When I do Login and Logout, it works perfectly, but when I’m logged in, it shows the object for a few seconds and then it disappears. When viewing the site in the editor it didn’t work, only on the published site. But in this case it shouldn’t be a problem with the code.
J.D
May 24, 2022, 9:27pm
5
You should make it hidden/collapsed on load.
When I login or logout, the code works. But when I’m logged (currentMember.getMember() into the site and I refresh the site page, that part of the code is called
} else {
$w("#PubliLogout").expand();
$w("#boxMemberLogoutGlobal").expand();
$w("#boxCardMemberLoginGlobal").collapse();}
and it doesn’t show that part
if(member) {
$w("#PubliLogout").collapse();
$w("#boxMemberLogoutGlobal").collapse();
$w("#boxCardMemberLoginGlobal").expand();
even after a few seconds and the “else” object being hidden it is shown and does not disappear.
This is the test site with what I reported: https://gusitepreview.wixsite.com/my-site
What could it be?
J.D
May 30, 2022, 3:23pm
7
I can’t tell.
Trying adding console.log’s and see what it logs.
Something like:
$w.onReady(() => {
currentMember.getMember().then(member =>{
console.log('member', member);
return handleUserState(member);
});
.catch(err => console.log(err));
authentication.onLogin(member => handleUserState(member))
})
//...