Doubt in Accounts.onLogin function


#1

I am trying to write the users time of login and their email address inside Accounts.onLogin method as shown below

Accounts.onLogin(function () {
    var fs = require("fs");
    var path = require("path");
    var filepath = "file.csv";
    csv_header = "E-mail,Last Login \n"
    if (!fs.existsSync(filepath)){
      fs.writeFileSync(filepath, csv_header);
    }
    var fd = fs.openSync(filepath, 'r+');
    fs.appendFileSync(filepath, Meteor.user().services.resume.loginTokens[0].when + "," + Meteor.user().emails[0].address + "\n");
    fs.closeSync(fd);
});

But this code executes multiple times when user logs in & thereby creating multiple records of single login. I would like to know three things from here
i) Why Accounts.onLogin executes multiple times?
ii) Is there any function which will execute only once after the user logs in
iii) Is there any better way to keep track of user login

Thanks in advance.


#2

Take a look at https://github.com/mizzao/meteor-user-status