I have a button hidden from members not logged in. Only members that are logged in can see the button.
The code seems to work perfectly only after manually refreshing the page. is there a way to force refresh the page? Currently, after login the code redirects the page to the homepage which i thought would fix the problem using the wixLocation.to, but getting redirected didn’t fix the refresh/button problem. Any suggestions?
Code:
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("#profileButton1").show();
}
else {
$w("#loginButton").label = "Courier Pickup";
$w("#profileButton1").hide();
}
} );
export function loginButton_click(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixLocation.to("/homepage");
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#loginButton").label = "Courier Pickup Login";
$w("#profileButton1").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;
wixLocation.to('/Members/' + userId);
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("#profileButton1").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}