After Login, Button Not Auto Hide / Show

I’m trying to do this article Velo Tutorial: Building Your Own Members Area | Help Center | Wix.com partially. By partially meaning I only want to use the hide / show function for user logged in / out.

So I want to show Wix Login Bar when user is logged in and hide it when user is logged out. When the Wix Login Bar is being hidden, it will be replaced by a button labelled Login.

But apparently the hide / show don’t work right away after a login / logout. I have to change page first for the function to commence. What did I do wrong?

Here is my code:

$w.onReady(function () {
	// On Mobile
	if(wixWindow.formFactor === "Mobile") {
		if(wixUsers.currentUser.loggedIn) {
		$w("#BoxLogiMobile").expand();
		$w("#MemberLogin").show();
	}
  	else
  	{
  		$w("#MemberLogin").hide();
  		$w("#BoxLogiMobile").collapse();
  	}
  	}
  	// On Desktop
  	else
  	{
  		if(wixUsers.currentUser.loggedIn) {
		$w("#LoginBUTTOn").hide();
		$w("#MemberLogin").show();
	}
  	else
  	{
  		$w("#MemberLogin").hide();
  		$w("#LoginBUTTOn").show();
  	}
  	}
});

The Wix Login Bar is #MemberLogin while #LogingBUTTOn is the replacement.

Hi,

You state that, “I have to change page first for the function to commence.” This makes me suspect that it’s just a matter of catching the onClick() of the login button to show/hide the appropriate elements. Your code is only in the onReady() function which means it only runs when the page is invoked. Any subsequent actions on the page won’t run this code again.

Yisrael

Hey Yisrael, sorry I have done a mistake regarding the button on click code. Just realize it when I wanted to reply your message. That problem has been solved.

But is it possible to trigger a code (show/hide or anything) automatically after a user logged in / out? I mean without having to do something first, after the login screen take place and the process is done, then the code is triggered.

Hi andlusim,

You can use onLogin for this:

wixUsers.onLogin( (user) => {
  $w('#text1').show();
});

Liran

Well onLogin works perfectly, but how about onLogout? I think there is no function for that.

If you have your own Logout button (not the built in one), it must have the following code:

export function logoutButton_click() {
    wixUsers.logout();
}

You get a promise that is resolved when a user has logged out, so just do:

export function logoutButton_click() {
    wixUsers.logout().then(() => {
        $w('#text1').hide();
    });
}

Liran.

The problem is I have to use Wix Login Bar, because Wix Contacts database isn’t accessible at all. Why not making an onLogout function? That will fix the hassle :slight_smile:

Could be :slight_smile: Feel free to add a feature request here .
But, you really don’t have to use the login bar.
You can just have a login and logout button of your own, and show and hide it:

wixUsers.onLogin( (user) => {
  $w('#text1').show();
  $w('#loginButton').hide();
  $w('#logoutButton').show();
});
export function logoutButton_click() {
    wixUsers.logout().then(() => {
        $w('#text1').hide();
        $w('#loginButton').show(); 
        $w('#logoutButton').hode();
    });
}

Liran.

@Liran Yes I can do that and in fact I have tried it after you mentioned it, it works fine. But the thing is I utilize Wix Forum and New Wix Blog which make me have Wix Member. If I don’t use their login bar, I can’t access several pages that a user need to access, such as the “My Setting” page, “My Profile” page, and the rest of Wix Member pages. The reason is that they use dynamic pages for each member and I can’t modify any of the code, thus I can’t point a DIY button to that specified page.

The complicated way is making my own forum and blog, which I have a total access to everything, but that is too much to even think about. The simplest thing that can be done is to have an onLogout function, so when I clicked on the logout button in Wix Login Bar some code are executed.

I definitely see your point.
Please do submit a feature request for that. We’re trying to make the best to attend all of them.

Liran.

Already done here https://www.wix.com/code/home/forum/feature-requests/onlogout-function

Bless you all!

I don’t really understand how I wright this out to get it to work on my site I am trying to hide my personalised login (#login) when logged in but show up when logged off and status update button (#Update) and my edit profile button (#Editprofile) to show when logged in but not if logged out