Enabling/disabling a button if a user is logged in

I want to make a button available only if the user is logged in. I used the following code and it works fine:

 if (wixUsers.currentUser.loggedIn) {
        $w("#specsheetdownload").enable(); // disable or enable
    } else {
        $w("#specsheetdownload").disable(); // disable or enable
    }

I’ve also seen code used a different way:

$w.onReady(function () {
  hideElem();
});

wixUsers.onLogin(() => {
  hideElem();
});

function hideElem() {
 let user = wixUsers.currentUser;
 let isLoggedIn = user.loggedIn;
 if (isLoggedIn) {
   $w("#specsheetdownload").enable(); // disable or enable    
   } else {      
   $w("#specsheetdownload").disable(); // disable or enable   
  }
}

Is one way better than thee other?

Hi Marc,

There’s no real better way to code. If it works for you, it works!

Sometimes people may prefer to use a shorter code like your original, just so it’s easier to read.

If the code you have works how you want it to work there is no need for you to change it.

Hope this helps!

Dara | Corvid Team

Thanks Dara! I was just curious if one way was better. Thanks for clearing that up!

Additional to Dara’s suggestions a little info 4 you.
On every friday you have the possibility to improfe your code, if you want to.
You will find “Friday’s-Review” just on fridays :grin: