Hello Coders! I would like for users to be redirected to a logout page when they click the site wide logout button. Could someone help me with that code?
top of page
Important forum update
This forum is migrating to one unified Wix community forum starting July 26th, and will be read-only during the process.
![feature fixed.png](https://static.wixstatic.com/media/3bfa8a_f845e1016b8a49758acdd6edd6610c3b~mv2.png/v1/crop/x_0,y_2,w_852,h_508/fill/w_104,h_62,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/feature%20fixed.png)
Wishlist Page is the official platform for requesting new features. You can vote, comment, and track the status of the requested features.
bottom of page
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:
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 } }); }
The code above only requires a simple dropdown named UserDropMenu to work.
I hope people find this info and code useful. Cheers.
Hello, Mauro Vento Avno & David White!
After some research, I came up with the code below which has been working as I intend for it to function. It lives in the 'Site Code' area. Of course, it'll need to be tweaked for your individual uses, but I hope that this gets you started!
import wixUsers from 'wix-users';
import wixData from 'wix-data';
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
$w.onReady(() => {
if (wixWindow.rendering.renderCycle === 1) {
if (wixUsers.currentUser.loggedIn) {
$w("#siteloginButton").label = "Logout";
$w("#siteProfileButton").show();
$w("#image13").show();
$w("#nameButton").show();
} else {
$w("#siteloginButton").label = "Login/Register";
$w("#siteProfileButton").hide();
$w("#image13").hide();
$w("#nameButton").hide();
wixLocation.to(`/Profile/Update/${wixUsers.currentUser.id}`);
}
}
});
export function siteloginButton_onclick() {
// user is logged in
if (wixUsers.currentUser.loggedIn) {
// log the user out
wixLocation.to('/logout');
wixUsers.logout()
.then(() => {
// update buttons accordingly
$w("#siteloginButton").label = "Login/Register";
$w("#siteProfileButton").hide();
$w("#image13").hide();
$w("#nameButton").hide();
});
}
// user is logged out
else {
let userId;
let userEmail;
// 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("Profile")
.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("Profile", toInsert)
.catch((err) => {
console.log(err);
});
}
// update buttons accordingly
$w("#siteloginButton").label = "Logout";
$w("#siteProfileButton").show();
$w("#image13").show();
$w("#nameButton").show();
wixLocation.to(`/Profile/Update/${wixUsers.currentUser.id}`);
})
.catch((err) => {
console.log(err);
});
}
}
export function siteProfileButton_click() {
wixLocation.to(`/Profile/Update/${wixUsers.currentUser.id}`);
}
Also need this
Right question, I'm in the same difficulties