Building a User Authentication Time Machine with the Slice State Manager

Ole Ersoy
Ole Ersoy
Nov 1 · 1 min read
Image by kalhh from Pixabay

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

First build a model used to track authentication times:

class UserAuthenticationTime {constructor(
public user: User,
public time: Date) {}
}

Our time machine is just an array:

const userAuthenticationTimeMachine:UserAuthenticationTime[] = [];

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);
}
});

We can now log the time machine array to see who has logged in and when:

console.log("Time Machine: ", userAuthenticationTimeMachine);
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade