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.