Hi,
Just a simple question about the Post-Login Action event object, specifically the event.authentication.methods (Event Object - Auth0 Docs). As the title says, I would like to know if this array is confirmed to be sorted by timestamp, as I couldn’t find an answer one way or the other.
Thanks for reading, any input is appreciated.
Hi @Sahid.Rosas
Welcome to the Auth0 Community!
There is no official guarantee or documentation confirming that the event.authentication.methods array is strictly pre-sorted by timestamp.
While the array often appears chronological in practice (since Auth0 appends authentication methods as they are successfully completed during the transaction), you should not rely on the array index (e.g., event.authentication.methods[0]) to determine the chronological order or the “first” login method.
The recommended approach would be to sort manually by the timestamp property.
You should explicitly sort the array yourself using the timestamp string property present on each method object:
exports.onExecutePostLogin = async (event, api) => {
if (event.authentication && Array.isArray(event.authentication.methods)) {
const sortedMethods = [...event.authentication.methods].sort((a, b) => {
return new Date(a.timestamp) - new Date(b.timestamp);
});
const firstMethod = sortedMethods[0];
const latestMethod = sortedMethods[sortedMethods.length - 1];
console.log(`First method used: ${firstMethod.name} at ${firstMethod.timestamp}`);
console.log(`Latest method used: ${latestMethod.name} at ${latestMethod.timestamp}`);
}
};
By adding a simple .sort() fallback, you guarantee your code remains 100% resilient regardless of any changes to the upstream JSON serialization logic.
Kind Regards,
Nik