/enrichLoginToken.js Secret
Created
June 24, 2023 07:13
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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