login problems for user profile

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}`); 

}

No time to review your code, but here is a little bit of what I’ve learned by playing with Wix Code since inception:
Never waste time with preview for anything , but especially regarding logins and member id, etc.
Completely useless for the task.
It’s ok for understanding and debugging early on in the learning curve, but useless for advanced endeavors.
Ymmv.
I only test the live site with various browsers.

Yes you can’t use login/signup on preview mode, only works properly testing on a live and published website.

As for your code, you are using the basics of this tutorial:
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area

Firstly, do you have the login button in your header or footer or just somewhere on your web page? If your login button is just on your page, then you just need to have the code in the ‘Page’ code. Otherwise if it is in either your header or footer, then place the code in your ‘Site’ code.

Anyways, for this code to work, you will need to have page refresh itself for the code to kick in and work.

I have similar on my members only page and I have used a custom lightbox for login which closes and refreshes my page after the member has logged themselves in.

This then allows the code to kick in and change the login button label to logout and show all the hidden member items etc, etc.

My login and profile button are in my header. I used the https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area to create my members area.

The issue is some users have no issue logging in. Others, even after multiple tries or refreshing page manually are not able to login or at least the button always says “Login” and “Profile” button never shows up.

Can you share how your custom light box works? Does user selects “Login” button and a custom lightbox pops-up and user enters email and password?