Picking up meteor.js user logout

前端 未结 6 1998
陌清茗
陌清茗 2020-12-30 01:52

Is there any way to pick up when a user logs out of the website? I need to do some clean up when they do so. Using the built-in meteor.js user accounts.

I\'ll be doi

6条回答
  •  一整个雨季
    2020-12-30 01:55

    The answer provided by @saimeunt looks about right, but it is a bit fluffy for what I needed. Instead I went with a very simple approach like this:

    if (Meteor.isClient) {
        Deps.autorun(function () {
            if(!Meteor.userId())
            {
                Session.set('store', null);
            }
        });
    }
    

    This is however triggered during a page load if the user has not yet logged in, which might be undesirable. So you could go with something like this instead:

    if (Meteor.isClient) {
        var userWasLoggedIn = false;
        Deps.autorun(function (c) {
            if(!Meteor.userId())
            {
                if(userWasLoggedIn)
                {
                    console.log('Clean up');
                    Session.set('store', null);
                }
            }
            else
            {
                userWasLoggedIn = true;
            }
        });
    }
    

提交回复
热议问题