import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w("#button4").label = "Logout";
$w("#button5").show();
}
else {
$w("#button4").label = "Login";
$w("#button5").hide();
}
} );
export function button4_onclick() {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w("#button4").label = "Login";
$w("#button5").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;
return wixData.query("MemberProfile")
.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("MemberProfile", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#button4").label = "Logout";
$w("#button5").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function button5_onclick() {
wixLocation.to(`/MemberProfile/Update/${wixUsers.currentUser.id}`);
}
top of page
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
bottom of page
I have been for a long while now, but can't find i'm doing anything different to what it saids. I thought a more experienced coder my just see it and could point it out. I appreciate its difficult without being in front of the screen. It was worth a try.
Instead of constant back and forth here, why not just follow the tutorial itself as that is where you have got the code from to use?
https://support.wix.com/en/article/corvid-tutorial-building-your-own-members-area
import wixUsers from 'wix-users'; import wixData from 'wix-data'; importwixLocation from 'wix-location'; $w.onReady( () => { if(wixUsers.currentUser.loggedIn) { $w("#loginButton").label = "Logout"; $w("#profileButton").show(); } else { $w("#loginButton").label = "Login"; $w("#profileButton").hide(); } } ); export function loginButton_click(event) { // user is logged in if(wixUsers.currentUser.loggedIn) { // log the user out wixUsers.logout() .then( () => { // update buttons accordingly $w("#loginButton").label = "Login"; $w("#profileButton").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; 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("#profileButton").show(); } ) .catch( (err) => { console.log(err); } ); } } export function profileButton_click(event) { wixLocation.to(`/Members/${wixUsers.currentUser.id}`); }
logout() refreshes the page and stops the code execution.
So:
wixUsers.logout() //This part won't run: .then( () => { // update buttons accordingly $w("#button4").label = "Login"; $w("#button5").hide(); } );
I can log in, but it doesn't let me log out and doesn't direct me to the dynamic page.