How to auto-refresh page onEvent

I’ve wrote the following code to show a box if a user is logged in only. When the user log in you have to manually refresh the page to reflect the changes. how to I make this automatic? When user log out the changes it works properly.

$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w(“#box1”).show();
}
else {
$w(“#box1”).hide();
}
} );

You can take a look here

if you want to refresh / reload a page look at wixLocation.to in the API Docs

You can use the wix-users.onLogin() function to refresh the screen.

Your page code should look something like this (quick and dirty example):

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixLocation from 'wix-location';

$w.onReady( () => {
    setupPage();
    wixUsers.onLogin( (user) => {
        setupPage();
    } );  
} );

export function setupPage() {
    if(wixUsers.currentUser.loggedIn) {
        $w("#box1").hide();
        $w("#box2").show();
    }
    else {
        $w("#box1").show();
        $w("#box2").hide();
    }
}

I hope this helps.

Yisrael

To let the page reload, use:

wixLocation.to('wixLocation.url');

This will send the user to the current URL, so that it reloads.

If i used a button to perform this function (the button would be OnClick) what code would i write under the event handler to reload the current page?

Thanks
Adam

@adcatch02 I believe I made a mistake, just put this underneath the event handler:

wixLocation.to('wixLocation.url');

Without the extra .to.

@webmasterq remove the quotation marks. It’s not a string.

@jonatandor35 Okay, but why? This works too.

@webmasterq because wixLocation.url is a variable with a string assigned to, not a string by itself.

@jonatandor35 I know what you mean, but if it works, it works.

@webmasterq but it doesn’t work…

Where I have to put this for reload ?:

wixLocation.to('wixLocation.url');

Please open a new thread and explain in more details when do you want to redirect.

Old thread. Closing it.