Skip to content

Instantly share code, notes, and snippets.

@msavin
Created June 24, 2023 07:13
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