Good day! I’m seeking for help to right my code. I Have a member login and when a member is already have an account it will go to the member profile dynamic page and if the member is new to my website, it will go to the create account dynamic page.
See my code below.
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
$w.onReady(() => {
if (wixUsers.currentUser.loggedIn) {
$w("#button14").label = "LOGOUT";
$w("#image29").show();
$w("#image68").show();
$w('#button11').disable();
$w('#buttonreg').disable();
} else {
$w("#button14").label = "LOGIN";
$w("#image29").hide();
$w("#image68").hide();
$w('#button11').enable();
$w('#buttonreg').enable();
}
});
export function buttons() {
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then(() => {
// update buttons accordingly
$w("#button14").label = "LOGIN";
$w("#image29").hide();
$w("#image68").hide();
$w('#button11').enable();
$w('#buttonreg').enable();
console.log("User logged out");
});
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin({
"mode": "signup"
})
.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("regdog")
.eq("_id", userId)
.find();
})
.then((results) => {
wixLocation.to(`/regdog/registered-dogs/${wixUsers.currentUser.id}`);
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail
};
wixLocation.to(`/regdog/create/${wixUsers.currentUser.id}`);
// add the item to the collection
wixData.insert("regdog", toInsert)
.catch((err) => {
console.log(err);
});
}
// update buttons accordingly
$w("#button14").label = "LOGOUT";
$w("#image29").show();
$w("#image68").show();
$w('#button11').disable();
$w('#buttonreg').disable();
})
.catch((err) => {
console.log(err);
});
}
}
$w(function () {
$w('#button14').onClick(buttons);
$w('#button11').onClick(buttons);
$w('#buttonreg').onClick(buttons);
});
I manage to figure it out in the first time but suddenly it’s not working anymore.
wixLocation.to(/regdog/registered-dogs/${wixUsers.currentUser.id}
);
-is for the member who already have an account to my website
wixLocation.to(/regdog/create/${wixUsers.currentUser.id}
);
-is for the new member to my website
See the image below.
I hope someone could help me on this.
Thank you,
Geo