- SOLVED - Stay on the same page on Log in if user is on a specific URL

Hi, I’m looking for a little guidance on the below code, what i’m trying to achieve is if the user is on the ‘/details-and-payment’ page and logs in to store the trip on their account, then it just closes the lightbox without redirecting them to their account page.

I would still like a user to be directed to their account page if the are NOT on the details and payment page.

export function loginButton_click(event) {

 let email = $w("#email").value;
 let password = $w("#password").value;

 wixUsers.login(email, password)
   .then( () => {
     console.log("User is logged in");
 if (wixLocation.url === ("/details-and-payment")) {
       wixWindow.lightbox.close
     }
 else { wixLocation.to("/account/payments");
       } 
   } )
    .catch( (err) => {
     console.log(err);
     $w("#errorMessage").expand();  
   } ); 
}

Thank you!

Hello Stephen,

The url looks at the whole string in the url bar, you want either path or prefix depending on your site structure.

Check out the image below for clarification


You have the right idea with the if statement but its not the url you want to compare.

Here is the full documentation:

Goodluck!
Majd

Hi @mqumseya , thank you for your response. I have had a play with the code but seem to be struggling, my coding skills are reasonable but I have never used this section of the API, could you point me in the right direction please? The code i have been playing with is below.

export function loginButton_click(event) {
 let path = wixLocation.path;
 let email = $w("#email").value;
 let password = $w("#password").value;

 wixUsers.login(email, password)
   .then( () => {
     console.log("User is logged in");
 if (wixLocation.path === path) {
       wixWindow.lightbox.close
     }
 else { wixLocation.to("/account/payments");
       } 
   } )
    .catch( (err) => {
     console.log(err);
     $w("#errorMessage").expand();  
   } ); 
}

Thank you!

Managed to finally sort this one!

Lightbox code:

export function loginButton_click() {
 let email = $w("#email").value;
 let password = $w("#password").value;
 if(wixUsers.currentUser.loggedIn) {
    wixUsers.logout()
      .then( () => {
 // update buttons accordingly
        $w("#loginButton").label = "Login";
    } );
  }
 else  wixUsers.login(email, password)
   .then( () => {
     console.log("User is logged in");
     $w("#loginButton").label = "Logout";
 if (wixLocation.url === ("https://www.scottishrockandwater.com/details-and-payment")) {
 const dataObj = {
        hello: 'world',
        hi: 'universe'
    };
     wixWindow.lightbox.close(dataObj);       
 }
 else { wixLocation.to("/account/my-adventures");
       } 
   } )
    .catch( (err) => {
     console.log(err);
     $w("#errorMessage").expand();  
   } ); 
}

Page code:

export function loginButton_click(event) {
wixWindow.openLightbox('Login').then(dataObj => {
        console.log(dataObj);
    })
}

If anyone has any cleaner code feel free to make it look prettier!

@stephenmccall Very nice! :slight_smile: