
Scenario
In the article:
We handled reactive state management concerns related to user authentication.
We now also want to have a time machine that gathers the user authentication times such that we can see what users logged into the application and when.
Approach
Model
First build a model used to track authentication times:
class UserAuthenticationTime {constructor(
public user: User,
public time: Date) {}
}
Time Machine
Our time machine is just an array:
const userAuthenticationTimeMachine:UserAuthenticationTime[] = [];Tracking
Whenever a new user signs in create a uat:UserAuthenticationTime instance and add it to the time machine.
observedUser.subscribe(user=> {
if (user) {
const uat:UserAuthenticationTime = new UserAuthenticationTime(user, new Date());
userAuthenticationTimeMachine.push(uat);
}
});Demo
We can now log the time machine array to see who has logged in and when:
console.log("Time Machine: ", userAuthenticationTimeMachine);