Skip to content

Instantly share code, notes, and snippets.

@pravinady
Last active April 6, 2020 23:28
Show Gist options
  • Save pravinady/7cc00bde775556cad89a58099aee484a to your computer and use it in GitHub Desktop.
Save pravinady/7cc00bde775556cad89a58099aee484a to your computer and use it in GitHub Desktop.
detect-login
function (user, context, callback) {
//Check if authn method match pwd or social connections
const authMethod = context.authentication.methods.find(
(method) => {
return (method.name === 'pwd' || method.name === 'federated');
}
);
console.log('auth method is:', authMethod);
if (!authMethod) {
console.log('skipping rule');
return callback(null, user, context);
}
var authnTimeStamp = authMethod.timestamp;
console.log('user authenticated at: ', authnTimeStamp);
var currentDate = new Date();
var currentEpochTimeStamp = currentDate.getTime();
console.log('current time is:', currentEpochTimeStamp);
var diffInSec = Math.trunc(((currentEpochTimeStamp - authnTimeStamp)/1000));
console.log('dif is:', diffInSec, ' secs');
//1 sec leeway for authentication
var leewayInSec = 1 * 1;
if(diffInSec < leewayInSec) {
console.log('recording events on login');
}
else {
console.log('skipping rule - user authentication time is greater than', leewayInSec, ' secs');
}
callback(null, user, context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment