First of all I am a newbie so please go easy. Here is the deal:
I am using the Wix member feature where users can sign up or log in. I am trying to direct users to a specific page for new members and a different page for current members. I have the code that works on occasion but not every time. I have pasted the code for the login screen for anyone that may be able to assist. Any advice would be greatly appreciated.
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w("#button9").label = "Log Out";
$w("#button10").show();
}
else {
$w("#button9").label = "Log In";
$w("#button10").hide();
}
} );
export function button9_click(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#button9").label = "Log In";
} );
}
// 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("CustomerList")
.eq("_id", userId)
.find();
} )
.then( (results) => {
if (results.items.length !== 0) {
wixLocation.to(`/my-account`);
}
// 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("CustomerList", toInsert)
.catch( (err) => {
console.log(err);
} );
}
wixLocation.to(`/CustomerList/Update/${wixUsers.currentUser.id}`);
// update buttons accordingly
$w("#button9").label = "Log In";
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function button10_click(event) {
wixLocation.to(`/my-account`);
}
Have you had any luck with this Anna? I'm trying to do the same thing...