Created
August 8, 2014 16:36
-
-
Save mhinton/fcd6347e5c51925086a2 to your computer and use it in GitHub Desktop.
Meteor user redirect on session expiration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// file: server/data.js | |
/* Expose extra fields to the client side */ | |
Meteor.publish("userData", function () { | |
return Meteor.users.find({_id: this.userId}, | |
{fields: {'tw': 1, 'tw_user': 1, 'roles': 1, 'name': 1, 'forceLogout': 1}}); | |
}); | |
// file: client/base.js | |
user_subscription = Meteor.subscribe("userData"); | |
// file: server/session.js | |
// | |
// periodically purge any stale sessions, removing their login tokens and clearing out the stale heartbeat. | |
// | |
Meteor.setInterval(function() { | |
var now = new Date(), | |
overdueTimestamp = new Date(now - inactivityTimeout); | |
var usersLoggedOut = Meteor.users.update( { heartbeat: { $lt: overdueTimestamp } }, | |
{ $set: { 'services.resume.loginTokens': [], forceLogout: true }, | |
$unset: { heartbeat: 1 } }, | |
{ multi: true }); | |
if (usersLoggedOut > 0) { | |
util.debug("==== users logged out: " + usersLoggedOut); | |
} | |
}, staleSessionPurgeInterval); | |
// file: client/session/session.js | |
Meteor.startup(function() { | |
// redirect users who have been logged out | |
Meteor.setInterval(function() { | |
var routePath = Router.current().path; | |
// no need to check at these URLs | |
if (_.include(['/login', '/logintw'], routePath)) | |
return; | |
Deps.autorun(function(){ | |
var user = Meteor.user(); | |
if (user && user.forceLogout) { | |
console.log("==== user has been logged out on the server redirect to /login"); | |
Meteor.users.update({_id: Meteor.userId()}, {$unset: {heartbeat: 1, 'forceLogout': 1}}); | |
Meteor.logout(); | |
Router.go('login'); | |
} | |
}); | |
}, heartbeatInterval / 2); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment