I had this code working a while ago… and then it just stopped.
There is no console log.
login/Logout labels switch but the click event is not working.
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w("#loginButton").label = "Logout";
$w("#cuenta").show();
} else {
$w("#loginButton").label = "Login";
$w("#cuenta").hide();
}
} );
export function loginButton_click(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#loginButton").label = "Login";
$w("#cuenta").hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query("Members")
.eq("_id", userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail
};
// add the item to the collection
wixData.insert("Members", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
$w("#cuenta").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function profileButton_click(event) {
wixLocation.to(`/profile/${wixUsers.currentUser.id}`);
}
export function image234_click(event) {
wixLocation.to(`/profile/${wixUsers.currentUser.id}`);
}
This snap is on the Live version.
I cant log in. I click the login btn and it does nothing.
This one is on the preview.
The only thing that is highlighted is the “then”
If there is any console-logs showing, you probably have disconnected your …
exportfunction loginButton_click(event){.....
in the wix-editors property-panel.
Please check this first and RECONNECT the “Log-Out-Button” with it’s related Log-out-function.
Nope…there are no console logs
login and logout button are the same button. The only thing that changes is the label
This function looks strange…
export function loginButton_click(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#loginButton").label = "Login";
$w("#cuenta").hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query("Members")
.eq("_id", userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail
};
// add the item to the collection
wixData.insert("Members", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
$w("#cuenta").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
What exacly you try to do with this function?
Log-Out or Log-In? Something else?
If the user is not logged in then show the login btn with login label
If the user is logged in then the login button show read logout and the account info is displayed.
Once you click on the login, then the login appears.
If you click on logout then you should be logged out.
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.
I’ll try it and let you know. 
Thanks!!!