Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove login tokens for meteor auth

I am not sure if this has been asked before but the loginTokens are getting really large and without any cleanup make my database increase in size. What is being done about this? what are others doing to manage this. I am referring to the default Meteor.users.services which has an array of loginTokens that get created every time the user logs in/out

"resume" : {
        "loginTokens" : [
            {
                "token" : "HMntXepqzPBLDGkGX",
                "when" : 1372559065392
            },
            {
                "token" : "uCHqA95HZZyN5tRtH",
                "when" : 1372563545565
            },
            {
                "token" : "sNGZhhATTrTg8582w",
                "when" : 1372622561176
            },
            {
                "token" : "hPWpm4uQQXWrkK6NS",
                "when" : 1372634411432
            },
            {
                "token" : "DFntTEcsKKT6bJ3rx",
                "when" : 1372635411745
            },
            {
                "token" : "BBM3acLQhuNtsHvkn",
                "when" : 1372638979158
            },
            {
                "token" : "EHgLLHMh6JWxKfuoe",
                "when" : 1372825386462
            }
        ]
    }
like image 533
Warz Avatar asked Jul 03 '13 20:07

Warz


1 Answers

This has been referenced several times on the Meteor google group but it's not a high-priority issue. In my authentication system, I delete tokens more than a day old whenever a user logs in. This ensures that the tokens don't expire if they haven't managed to log in for a while.

Accounts.registerLoginHandler (loginRequest) ->

  # ... Do whatever you need to do to authenticate the user

  stampedToken = Accounts._generateStampedLoginToken();
  Meteor.users.update userId,
    $push: {'services.resume.loginTokens': stampedToken}

  # Delete old resume tokens so they don't clog up the db
  cutoff = +(new Date) - (24*60*60)*1000
  Meteor.users.update userId, {
    $pull:
      'services.resume.loginTokens':
        when: {$lt: cutoff}
  },
  {multi : true}

  return {
    id: userId,
    token: stampedToken.token
  }
like image 127
Andrew Mao Avatar answered Sep 27 '22 16:09

Andrew Mao