Redirect to home page after logout

Question:
How do I redirect to the home page after logout? I have added the code but it redirects to the default login page after logout

What have you already tried:
I have added the below code in my master.js file so that it can work on any page.

import wixLocation from 'wix-location';
import {authentication} from 'wix-members-frontend';

authentication.onLogout(() => {
  console.log('Member logged out');
  wixLocation.to("https://www.1ezclick.com");
});

Normaly after logOut → your site restarts with your setted-up staarting page automatically.
Didn’t you already setted-up your HOMEPAGE as starting page?

I have set my page as homepage.

where is starting page option ?

My statement was not completely correct.

When you do a LOGOUT, out of my knowledge you stay at the same page (AS LONG AS THE PAGE IS NOT A PW-PROTECTED ONE, OR HAS OTHER RESTRICTIONS REGARDING ACCESSIBILITY).

Example:
a) loging out on a PW-secured page → will leave the current page
b) loging-out on an ordinary page ----> you will stay at the current page (no navigation to another page)

But what is 100% sure is, that after logout-process the page REALOADS, that means your code can not work.

authentication.onLogout(() => {
  console.log('Member logged out');
  wixLocation.to("https://www.1ezclick.com");
});

I think even when you try this one… it still won’t work…

authentication.onLogout(()=> {console.log('Member logged out');})
.then(()=>{wixLocation.to("https://www.1ezclick.com"); });

You could use the Wix-Storage for your puspose to save a command for navigation inside of wix-storage and then when your page (MASTER-PAGE) loads you check for the saved command inside of your STORAGE first and see if a command has been left to do a navigation.

Awww smart !

can you give a code snippet?

I am trying but not working when logout’s

See this code in masterpage.js

import {authentication} from 'wix-members-frontend';
import wixLocationFrontend from 'wix-location-frontend';
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import {local} from 'wix-storage-frontend';


$w.onReady(function () {
  	if(local.getItem("login") == 'true'){
      console.log('redirected to member\'s page');
  	}
    else if(local.getItem("login") == 'false')
    {
      local.removeItem("login");
      console.log('redirected to home page');
      wixLocation.to("https://www.1ezclick.com");    
    }
});

authentication.onLogin(async (member) => {
  // const loggedInMember = await member.getMember();
  // const memberId = loggedInMember._id;
  // console.log(`Member ${memberId} logged in:`, loggedInMember);

  wixLocationFrontend.to("/account/businfo");
  local.setItem("login", "true");
});

authentication.onLogout(() => {
  console.log('Member logged out');
  local.setItem("login", "false");

});

First of all —> your onLogin()-function must be inside of the $w.onReady()-event !!!
Second —> this do not exist → wixLocationFrontend.to

You should have generated something like…

import {authentication} from 'wix-members-frontend';
import {local} from 'wix-storage-frontend';

$w.onReady(()=> {console.log('Page is ready...');
	//...your check-function first.....
	checkLoginState();

	authentication.onLogin(async(member)=> {console.log('MEMBER: ', member);
		const loggedInMember = await member.getMember(); console.log('Logged-In-MEMBER: ', loggedInMember);
		const memberId = loggedInMember._id; console.log('Member-ID: ', memberId);
		console.log(`Member ${memberId} logged in:`, loggedInMember);
		//start REDIRECTION....
		wixLocation.to("/account/businfo");
	});

	authentication.onLogout(()=> {console.log('Member logged-out');});

	$w('#logInButton').onClick(()=>{local.setItem("login", "true"); 
		let email = ????????????????????????;
		let password = ?????????????????????;
		logIn(email, password);
	
	});
	$w('#logOutButton').onClick(()=>{local.setItem("login", "false"); authentication.logout();});

	//After clicking the Log-Out-Button --> the page should RESTART!
	//Now you need to implement a CHECK-FUNCTION, which will check for the saved value inside of the STORAGE!
	//EXPAND YOUR CODE, to be able to CHECK for STORAGE-VALUE on LOAD of the page, if "login", has been set to TRUE or FALSE.........

});


function checkLoginState() {
	return local.getItem("login");
}

function logIn(email, password) {
	authentication.login(email, password)
  	.then(()=> {console.log('Member is logged in');})
  	.catch((err)=> {console.error(err);});
}

i tried your code

It is still not redirecting to home page after logout.

Hey!

Just want to jump in and say this code is almost there, I believe it just needs to be wrapped within the onReady to work fully

import wixLocation from 'wix-location';
import {authentication} from 'wix-members-frontend';

$w.onReady(function () {
authentication.onLogout(() => {
  console.log('Member logged out');
  wixLocation.to("https://www.1ezclick.com");
});
});

The wix-members-frontend can only be used once the page has loaded, therefore must be used in the onReady.

Double check it’s in the masterPage.js also :slight_smile: