Check if user is logged in



  • I would like to check, if user is logged in from Javascript in Custom JS, like this:

    $(window).bind("load", function() {
        if (/* the current user is logged in */) {
            $(".myclass").css("display", "none")
        }
    })
    

    How can I accomplish this?


  • Admin

    if (app.uid) {
       console.log('logged in')
    }
    


  • @baris said:

    if (app.uid) {
       console.log('logged in')
    }
    

    For some reason is the app.uid always null if called in $(window).bind("load", ...).


  • Admin

    Ahh yeah it will be null until the client receives event:connect. So you might try

    socket.on('event:connect', function(data) {
     if (data.uid) {}
    });
    
    


  • Ok, thanks, this works. Now related question, how can I hide a whole category for registered users? (I know how to do it for non-registered users)


  • Admin

    You can do it from ACP>manage>categories>access control.

    You need to give the find privilege to the groups that you want to see the category, in this case give to all groups except registered-users.



  • @baris said:

    You can do it from ACP>manage>categories>access control.

    You need to give the find privilege to the groups that you want to see the category, in this case give to all groups except registered-users.

    It does not work, it seems that if I set some privileges to "guest" group, than every one get this privileges.



  • @3malG said:

    @baris said:

    You can do it from ACP>manage>categories>access control.

    You need to give the find privilege to the groups that you want to see the category, in this case give to all groups except registered-users.

    It does not work, it seems that if I set some privileges to "guest" group, than every one get this privileges.

    Now, it simply do not work for administrators, it is seems not to be possible to remove "find" privilege from administrator group.



  • @3malG said:

    Now, it simply do not work for administrators, it is seems not to be possible to remove "find" privilege from administrator group.

    That would stop you being able to see that category. May as well not have the category.



  • @3malg @baris

    User id is stored in app.user.uid - app.uid is always undefined.
    Not sure whether this has changed over the years but this thread appeared when I googled something like "nodebb find out if user is logged in" - so might help someone else.


  • Admin

    @joes Yes app.uid got removed quite a while ago, the user data is stored in app.user


 

| |