Try this one. Test it first.
If it works for you, add & modify the code by your further needs.
import wixUsers from 'wix-users';
$w.onReady(async function(){
let user = wixUsers.currentUser; console.log("Wix-User-Data: ", user)
let userId = user.id; console.log("User-ID: ", userId)
let isLoggedIn = user.loggedIn;
if(isLoggedIn) {console.log("User already logged in! Changing BTN-Label to ---> Log-Out")
$w('#loginButton').label = "Log-Out", $w("#cuenta").show('fade');
}
else{console.log("User not logged in! Changing BTN-Label to ---> Log-In")
$w('#loginButton').label = "Log-In", $w("#cuenta").hide('fade');}
$w('#loginButton').onClick(()=>{console.log("Log-In-Button clicked!")
if(isLoggedIn) {logOut();}
else{promptLogin();}
});
});
function promptLogin() {console.log("Log-In running...")
wixUsers.promptLogin()
.then((user) => {
let userId = user.id; console.log("User-ID: ", userId);
let isLoggedIn = user.loggedIn; console.log("User-LogIn-State: ", isLoggedIn)
return user.getEmail();
})
.then((email)=> {
let userEmail = email; console.log("User-Email: ", userEmail)
})
.catch((err)=>{let errorMsg = err; console.log(errorMsg), console.log("Something gone wrong!")});
}
function logOut() {console.log("Log-Out process running...")
//$w("#cuenta").hide(); //<--- this should be setted up in the wix-editor as current state
//$w('#loginButton').label = "Log-In" //<--- this should be setted up in the wix-editor as current state
wixUsers.logout();
}
As you can see, i implemented the Login-Button-Function directly into the Sw.onReady-code-part. Please pay attention on it. You won’t need the setted-up connection in the wix-editors properties-panel anymore.