Redirect to a static page at Logout

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?

2 Likes

Right question, I’m in the same difficulties

Also need this

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});
}

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.