Last Updated: September 09, 2019
· benjaminrh

Meteor user login/logout hooks!

It's time. The time we've all been waiting for. That's right, it's Mort Fun Time! Free pots of nerd-cred to anybody who doesn't need the link to know what I'm talking about :)

Seriously though, I've been wanting hooks for when a user logs in/out for a while now, as have a number of other Meteorites. It's been mentioned before, and people have recommended different things all over the place. I filed one too the other day.

Since I'm making a package of hooks for various events (I posted that before, but decided this part deserved it's own little post, since so many people have been asking me about it), this seems like a very logical thing to include. Also I'm not very patient. So:

Hooks.onLoggedIn = function () {
    // this runs right after a user logs in, on the client or server
Hooks.onLoggedOut = function () {
    // this runs right after a user logs out, on the client or server

Also, because some people want to do logout things when a user exits the browser, I've added an option that allows you to fire the onLoggedOut when onCloseSession is triggered.

Hooks.treatCloseAsLogout = true; // somewhere on the client

You can install it with mrt add event-hooks. You can also check out the source (see what I did there?).

3 Responses
Add your response

This is awesome, thank you.

over 1 year ago ·

Ah, many thanks for making this!

over 1 year ago ·

if i want to add a hook right before a user logs in, what should i do?

over 1 year ago ·