Logout member: redirect to home

Hi everyone.
I created a member area: a “my account” page and a “account navigation bar” were automatically added to my site.
My issue is that.
When a member logout and he is in the “my account” page the log in page appear and clicking the X the browser stay on the same page, but all is white because the user logged out haven’t permission to see the page.
Same issue with all the others member pages.
Is there a way to redirect users to the home page when they logout from a member pages?

Thanks

1 Like

As the code below shows you when the user is logged out that function is executed and a promise is returned. On the line where the code below do console.log that’s where you would put your wixLocation.to(“url-to-redirect-to-here”);

wixUsers.logout() .then( ( ) => {
     console.log("User logged out"); 
} ) .catch( (err) => {
 let errorMsg = err; // "No member is logged in" 
} );

Thaks for the reply Andreas, but i’m not very good with coding.
Could you explain me better, please?

Hey Andreas,
I tried your code but the issue is that the thing does not stay on the home page after logging out. It redirects back to a login page that takes you to the same old page that you logged out from.

function PageTimeOut() {
//setTimeout(() => {wixLocation.to(“/”), wixUsers.logout();}, 9000);
setTimeout(() => {wixUsers.logout() .then( ( ) => {
wixLocation.to(“/”);
} ) . catch ( (err) => {
let errorMsg = err; },200); // “No member is logged in”
console.log(“User logged out”);}, 8000);
}

Hey
wixUsers.logout delivers a promise as you can read about in the API reference. So after that function has delivered it’s promise then you can do the redirect to the page you want. If you do not handle the promise the logout will go back to the login page because your users are still on the same page that requires logging in.

So when you do

wixusers.logout()
.then(() => {
wixLocation.to(“/”);
});

That will log them out, wait for that to happen and then redirect them to the root page in your site. Just remember to redirect them to a page that does not demand logging in.

Hello again,

Thanks a lot for the reply. I tried what you said. No luck.

function PageTimeOut() {
setTimeout(() => {wixUsers.logout()
.then(() => {
wixLocation.to(“/”);
});}, 9000);
}
It just logs out and stays on the same page asking the user to login.
I’m using the setTimeout() function does that affect this? See the issue here:

How do you call the PageTimeOut function?

I call it in the on-ready function. Here’s the full code:


import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
export function input4_keyPress(event, $w) {
    $w("#input4").onCustomValidation( (value, reject) => {
 if(value !== $w('#input2').value) {
      $w("#text109").show();
    reject("Emails do not match. Please try again.");
  }
 else{
       $w("#text109").hide();
  }
} );
}
export function button1_click(event, $w) {
    $w("#image17").show();
}
let dateStr;
var val=0; //unnecessary. I, @Vipul Kulshreshtha, used it as a test variable.
export function checkbox1_change(event, $w) {
 const today = new Date();
 if ($w('#checkbox2').checked && $w('#checkbox1').checked && dateStr === (today.toDateString()) && ($w('#input2').value === $w('#input4').value)) {
         $w('#button1').enable();
         $w("#text103").hide();
         $w("#text109").hide();
     } else {
         $w('#button1').disable();
         $w("#text103").show(); 
     }
}
export function checkbox2_change(event, $w) {
 const today = new Date();
 if ($w('#checkbox2').checked && $w('#checkbox1').checked && dateStr === (today.toDateString()) && ($w('#input2').value === $w('#input4').value)) {
         $w('#button1').enable();
         $w("#text103").hide();
         $w("#text109").hide();
     } else {
         $w('#button1').disable();
         $w("#text103").show(); 
     } 
}
function Redirect() {
    wixLocation.to("/");
}
function PageTimeOut() {
    setTimeout(() => {wixUsers.logout()
.then(() => {
  wixLocation.to("/");
});}, 9000);
setTimeout(() => {Redirect()}, 9500);
}

//$w.onReady(function () { //Disabling normal on-ready function
$w.onReady(() => {
    PageTimeOut();
    $w("#button1").disable(); //Disable the show QR button initially. 
 const today = new Date(); // Gets today's date
            $w("#datePicker1").onChange( (event, $w) => {
 let newValue = event.target.value;
            dateStr = newValue.toDateString(); // "new value"
 if (dateStr === (today.toDateString())) //check if date retrieved via datepicker object is a valid date and matches today's date.
            {
                $w("#text102").hide();     //hide error message
                val=1;
            } 
 else {
                $w("#text102").show();    //show error message
                $w("#button1").disable(); //deactivate show QR button
                val=0;
            }
            }
 

        );
    }
);


I tried putting a separate redirect() function but no use.

Add that code to the buttom of your onReady instead

setTimeout(() => {wixUsers.logout() .then(() => { wixLocation.to(“/”); });}, 9000);

and also put in the full url not just the / and test that. So no function PageTimeOut just add the timeout code in the last lines of your onready and also the full url like https://www.domain.com/

Do you mean that I don’t use the function and add the code inside the onReady function in the last?

Yes

Tried. The issue persists. It logs out but stays on the page asking to log in.

$w.onReady(function () {
//$w.onReady(() => {
 //PageTimeOut();
    $w("#button1").disable(); //Disable the show QR button initially. 
 const today = new Date(); // Gets today's date
            $w("#datePicker1").onChange( (event, $w) => {
 let newValue = event.target.value;
            dateStr = newValue.toDateString(); // "new value"
 if (dateStr === (today.toDateString())) //check if date retrieved via datepicker object is a valid date and matches today's date.
            {
                $w("#text102").hide();     //hide error message
                val=1;
            } 
 else {
                $w("#text102").show();    //show error message
                $w("#button1").disable(); //deactivate show QR button
                val=0;
            }
            }
 

        );
    setTimeout(() => {wixUsers.logout() .then(() => {   wixLocation.to("https://www.rat-store.com"); });}, 9000);
    }
 
);

Ok redirect them to the home page without logging out and send a query string with the url

wixLocation.to(“/?logout=true”);

Then check in the onReady on the homepage if query.logout is true and if it is log them out on the home page code instead. Might work more solid.

I think it might be because the function onReady belongs to that specific page (in this case https://www.rat-store.com/manual-pay-rat). Is there a way so that on any page when a user logs out it will return to home page no matter what.

Ok, let me do that. I think that might work.

A small help. Could you tell me how to pass the query to the home page? I am not able to determine the syntax.

It’s working! I figured it out! Hooray! Thanks a lot @Andreas :smiley:

Hi All,

I’m fairly new to Wix and notice that answers can be hard to find for certain issues.
With the help of the good folk on here and much thought, I’ve been able to figure out most things Corvid.

This issue seems to come up a lot so I thought I’d throw my 2 cents in for a simple solution and furthermore, a simple helpful feature.

wixLocation.to(‘/’)
wixUsers.logout().setTimout(500);

The code above seems to redirects the user then log them out but in function and because of Asynchronous code operation, the logout ends up occurring first.

By setting the timeout (half a second here), the logout function execution is delayed. This can be increased or reduced.

One simple but handy function is a dropdown with redirection to various pages or other functions (use an if statement to assess the selection for other functions, and include a logout.

Here is the example:

/**CODE/

import wixLocation from ‘wix-location’;

$w.onReady(function (event) {

InitiateUserMenu()          

});

export function InitiateUserMenu(){ //CALLED FROM THE ONREADY() ABOVE.

let MenuOptions = [ //SET THE ITEMS IN THE DROPDOWN MENU

    {"label": "MenuName1", "value": "RedirectToAddress1"}, 
    {"label": "MenuName2", "value": "RedirectToAddress2"}, 
    {"label": "Logout", "value": '/'} 

]; 
$w('#UserDropMenu').placeholder = "Placeholder";   //PLACEHOLDER CAN BE THE USER'S NAME 

$w('#UserDropMenu').options = MenuOptions;         //LOAD THE MENUOPTIONS VARIABLE IN 

$w("#UserDropMenu").onChange( (event, $w) => {    //WHEN ITEM IS SELECTED DO THE FOLLOWING 

if($w("#UserDropMenu").value === '/'){       //IF VALUE OF SELECTED IS THAT OF HOME, LOG US OUT 

        wixLocation.to($w("#UserDropMenu").value)    //REDIRECT TO HOME 
        wixUsers.logout().setTimout(500);                    //LOGOUT USER WITH A HALF SECOND DELAY    

    }else{ 

        wixLocation.to($w("#UserDropMenu").value);   //REDIRECT TO THE VALUE OF SELECTED ITEM 

    } 

}); 

}
/**END CODE/

The code above only requires a simple dropdown named UserDropMenu to work.

I hope people find this info and code useful. Cheers.

Hello,

I have added a ‘Members Area’ to my site. It is just one page that site users have to log in to see. That part works well, but once the visitor has logged in and is on the member page, there is no way for them to log out. If they leave the member page, or site, they never have to log in again to access the page. I would prefer that the visitor has to at least log in each time they wish to access this member page. Any ideas? Can I add some kind of time out feature, or can I force the user to log in each time they want access?

Am not a programmer, so any help would be appreciated!!!

Cheers,
Jana