Quick Tidbit for managing lastLogin in Coffeescript

Here’s a simple way to handle setting last Login field for your meteor app, using statusEvents inspired by: meteor-user-status, it naively assumes that you’re status is set to onLine if you login, but obviously no management there. I was using
this https://github.com/mizzao/meteor-user-status but ran into problems with schema2, see: https://github.com/mizzao/meteor-user-status/issues/91

@statusEvents = new (Npm.require('events').EventEmitter)()

Accounts.updateConnectionStatus  = ( info ) ->
  Meteor.users.update( { _id: info.userId } , 
          lastLogin: info.lastLogin, 
          lastLoginConnectionId: info.connectionId,   
          lastLoginIpAddress: info.ipAddr
          lastLoginUserAgent: info.userAgent
          online: true

#Server add this event emitter listener:
statusEvents.on “connectionLogin”, (info) ->

#Then in your onLogin callback add this:
statusEvents.emit “connectionLogin”,
userId: info.user._id
connectionId: info.connection.id
ipAddr: info.connection.clientAddress
userAgent: info.connection.httpHeaders[‘user-agent’]
lastLogin: new Date()
return info

1 Like