Skip to content

Instantly share code, notes, and snippets.

@msavin
Created June 24, 2023 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msavin/45995647be30a48bc0d21856466ff2f0 to your computer and use it in GitHub Desktop.
Save msavin/45995647be30a48bc0d21856466ff2f0 to your computer and use it in GitHub Desktop.
const enrichLoginToken = (userId, userAgent) => {
const user = Meteor.users.findOne(userId);
if (!user) {
return console.log(`User ${userId} not found`);
}
const loginTokens = user?.services?.resume?.loginTokens;
if (loginTokens && loginTokens.length !== 0) {
return console.log(`User ${userId} does not have any login tokens`);
}
// Get the latest login token
const lastLoginToken = loginTokens[loginTokens.length - 1];
// Enrich it with some extra fields
const updatedLoginToken = {
userAgent: userAgent,
sessionId: Random.id(),
createdAt: new Date(),
...lastLoginToken
};
// Replace the latest login token with the updated one
loginTokens[loginTokens.length - 1] = updatedLoginToken;
Meteor.users.update(userId, {
$set: {
'services.resume.loginTokens': loginTokens
}
});
}
Accounts.onLogin(function(info) {
const userId = info.user._id;
const userAgent = info.connection['user-agent']
enrichLoginToken(userId, userAgent)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment