I have the below code under “Site”. It allows user to sign-up or login into the website. If user info is not present in the table “Site_Members”, we add the data (_id and email).
In the preview mode this works ok. If some of my users trying to log-in the prompt for login in working. But after user logs-in nothing happens. The Buttons stills say “login” and user never able to log-in. If user switches browsers or uses a different phone they are able to log-in.
Any idea why user is not able to login?
I think this issue also happens when user tries to sign-up
An help is really appreciated.
Thank you.
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
import wixData from ‘wix-data’;
import wixWindow from ‘wix-window’;
let existingUser = “NO”;
//After sigining up user info is not filled ouf Use this to indicate its not filled out
$w.onReady(() => {
if (wixUsers.currentUser.loggedIn) {
$w(“#loginButton”).label = “Logout”;
$w(“#profileButton”).show();
wixData.query(‘Site_Members’)
.eq(‘_id’, wixUsers.currentUser.id)
.find()
.then(result => {
let item = result.items[0];
console.log(item);
if (item.paid_member === true ) {
//$w(“Members Information”).show();
}
}). catch ((err) => {
$w(“#errorMsg”).text=err;
console.log(err);
});
}
else {
$w(“#loginButton”).label = “Login/Sign-up”;
$w(“#profileButton”).hide();
}
});
export function loginButton_click(event) {
// check if user is logged in
if (wixUsers.currentUser.loggedIn) {
// log out the user
wixUsers.logout()
.then(() => {
wixLocation.to(/Home
);
// update buttons accordingly
$w(“#loginButton”).label = “Login/Sign-Up”;
$w(“#profileButton”).hide();
})
. catch ((err) => {
$w(“#errorMsg”).text=err;
console.log(“error1”+err);
});
}
// user is logged out
else {
console.log(“Logged out”);
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(“Site_Members”)
.eq(“_id”, userId)
.find();
})
.then((results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create a profile with just ID and email
const toInsert = {
“_id”: userId,
“email”: userEmail
};
// add the item to the collection
wixData.insert(“Site_Members”, toInsert)
. catch ((err) => {
console.log(“error2”+err);
});
} else {
session.setItem(“existingUser”, true );
existingUser = “YES”
}
// update buttons accordingly
$w(“#loginButton”).label = “Logout”;
$w(“#profileButton”).show();
let currentuserId = wixUsers.currentUser.id;
wixLocation.to(/Site-Members/Profile/${wixUsers.currentUser.id}
);
})
. catch ((err) => {
$w(“#errorMsg”).text=err;
console.log(“error3”+err);
});
}
}
export function profileButton_click(event) {
let results;
let item
wixData.query(‘Site_Members’)
.eq(‘_id’, wixUsers.currentUser.id)
.find()
.then(result => {
let items = results.items;
item = items[0];
console.log(item);
});
if (session.getItem(“existingUser”) === false ) { //if(existingUser === “NO”))
wixLocation.to(/Site-Members/Profile-Update/${wixUsers.currentUser.id}
);
}
wixLocation.to(`/Site-Members/Profile/${wixUsers.currentUser.id}`);
}