Help! Need Refresh After Login

Hi,
I have searched around for many code samples an cannot seem to get my page to refresh after a user logs in, forcing the Logout to change to Login - I need to manually refresh the page and it works fine. I have read the PromptLogin should be doing this but it is not … Here is the code I have pieced together through samples I have found. I also cannot seem to get it to redirect after login to my members page: https://slimpossiblenutrit.wixsite.com/mysite-1/members

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#login”).label = “Logout”;
$w(“#profile”).hide();
$w(“#mainmenu”).show();

}
else {
$w(“#login”).label = “Login”;
$w(“#profile”).hide();
$w(“#mainmenu”).hide();
}
} );

export function login_click() {
// if a user is already logged in
if (wixUsers.currentUser.loggedIn) {
// go to home page
wixLocation.to(“/”);
// log the user out
wixUsers.logout()
.then( () => {
// and update buttons accordingly
setButtons( true );
} );
}
// if a user is already logged out
else {
let userId;
let userEmail;

// then 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(“Users”)
.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(“Users”, toInsert)
. catch ( (err) => {
console.log(err);
} );

  } 

else
// update buttons accordingly
setButtons( true );
wixLocation.to(“https://slimpossiblenutrit.wixsite.com/mysite-1/members”);
} )
. catch ( (err) => {
console.log(err);
} );
}
}

Thank you!!!

I think you can solve the flow by adding a console.log(“STEP 1”); and step 2 so you can see why the redirect does not happen. I think the returns might be wrong and you might be forced to AWAIT the actions or promises to wait for all data and then redirect.

If you need help get back but console.log is a great way to see where the code breaks.

Ok, thank you ! I will try that … I did end up getting the login to redirect since I posted this and the text does change but now not changing logout back to login when I logout lol … getting there but Ill try your suggestion above, thank you!

Still having some trouble (I am not a coder, so bare with below :slight_smile: The login works great, now I cannot get it to logout / at one point it would logout an just not change the button back but now it just wont logout / any suggestions? Thank you!!!

BTW - I found this code through another post , I dont have the setButtons defined so not sure if that is tripping it up (?) I thought I could get the same results from the beginning onReady code.

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady( () => {
if (wixUsers.currentUser.loggedIn) {
$w(“#login”).label = “Logout”;
$w(“#profile”).hide();
$w(“#mainmenu”).show();

}
else {
$w(“#login”).label = “Login”;
$w(“#profile”).hide();
$w(“#mainmenu”).hide();
}
} );

export function login_click() {
// if a user is already logged in
if (wixUsers.currentUser.loggedIn) {
// go to home page
wixLocation.to(“https://slimpossiblenutrit.wixsite.com/mysite-1/”);
// log the user out
wixUsers.logout()
.then( () => {
// and update buttons accordingly
// setButtons(true);
} );
}
// if a user is already logged out
else {
let userId;
let userEmail;

// then 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(“Users”)
.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(“Users”, toInsert)
. catch ( (err) => {
console.log(err);
} );
}
else
// update buttons accordingly
// setButtons(true);
wixLocation.to(“https://slimpossiblenutrit.wixsite.com/mysite-1/members”);
} )
. catch ( (err) => {
console.log(err);
} );
}
}