Create a lockscreen feature in AngularJS that becomes active after a period of inactivity

I am looking to integrate a lockscreen feature into my app using Angular.js. This lockscreen will consist of a route and an HTML template containing a form that prompts the user to re-enter their password in order to keep their session active.

The purpose of this lockscreen is to prevent unauthorized access if a user leaves their computer or session unattended for an extended period of time.

My goal now is to have Angular execute this action after a specified amount of time of user inactivity. Any suggestions on how to achieve this?

Edit:
Please note that I am not interested in a jQuery solution that reloads the page after a certain period of inactivity, as this would disrupt the user's workflow.

Answer №1

To enhance security, it is important to implement this feature on the server using sessions. This prevents unauthorized users from making requests pretending to be someone who was previously logged in and active.

I'm looking for a way to make Angular handle this task after a certain period of user inactivity. Any suggestions?

Angular provides the $interval service, which can be used to execute a task at regular intervals. However, capturing all events within a document, such as detecting if a user has walked away or switched tabs, is more complex.

An alternative method is to monitor mouse movement. Below is an adaptation of this solution for the Angular framework:

[ See it working in a JSFiddle. ]

HTML

<div ng-app>
    <div ng-controller="IdleWatch">
        <span ng-show="!isIdle()">User not idle</span>
        <span ng-show="isIdle()">User idle</span>
    </div>
</div>

JavaScript

function IdleWatch ($scope, $interval, $document) {
    var int = null;
    var callbacks = [];
    var idleTime = 0;

    $scope.isIdle = function() {
        return (idleTime > 0);
    };

    angular.element($document).bind('mousemove', function(e) {
        idleTime = 0;
        $interval.cancel(int);
        startInterval();
        $scope.$apply();
    });

    function startInterval() {
        int = $interval(function() {
            idleTime += 3000;
        }, 3000);
    }

    startInterval();
}

Answer №2

I recently incorporated the ng-idle directive into my project, and I must say it offers a plethora of useful features in a user-friendly manner. Check it out here: https://github.com/hackedbychinese/ng-idle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The state may be modified, but the component remains unchanged

I've been tasked with implementing a feature on a specific website. The website has a function for asynchronously retrieving data (tickets), and I need to add a sorting option. The sorting process occurs on the server side, and when a user clicks a s ...

Is there a way to detect when a CSS background image has finished loading? Does an event trigger upon completion?

I am facing an issue with my sidebar widget where there is an image background in place. On top of this, I have a search input form but I don't want the input to display until the image has fully loaded. Is there a way to add a load event handler to ...

Deactivating a Vue custom filter when hovering over it

I'm trying to figure out how to disable a truncate filter when hovering over an element using VueJS 2. Here's the part of my template that includes the filter: <div class="eng" @mouseover="showAll">{{ word.english | truncate }}</div> ...

The Javascript setTimeout Function Prolongs Its Operation

Currently, I have a web app index page that performs two main tasks. Firstly, it geocodes the user's location and stores it in a JavaScript variable called "variableToSend." Secondly, it sends this data to mySQL using a PHP script after a delay of 10 ...

Dynamically populate 7 select boxes with options using JQuery

I have a webpage that includes 14 different dropdown menus, one for each day of the week (Monday to Sunday). Each day has two dropdowns: one for opening time and one for closing time. I used jQuery to populate all 14 dropdowns with a pre-defined list of ho ...

Personalize the error message for throwing an exception in JavaScript

I've been working on customizing error messages for exceptions thrown in JavaScript. Despite my best efforts, I have not been successful so far. I'm currently attempting the following code snippet but it's not functioning as expected: f ...

Is there a way to ensure that an external file function completes before proceeding with the main code execution?

In my project, I have two key JavaScript files: main.js and load.js. Within load.js, there are two essential functions - getData, which makes an AJAX post call, and constHTML, which appends data based on the JSON array returned from the AJAX call. This app ...

Iterating over an array of objects and executing asynchronous operations

I am currently working with NextJS and managing an array of assets (images) within my state. The task at hand is to post these images to the API. To accomplish this, I have a specific object that handles the posting process using the following syntax: let ...

The rsuite table does not properly reflect changes in state data

This is the render method that I am working on render() { return ( <Contentbox> <ol> {this.state.data.map((obj) => ( <li key={obj._id}>{obj.name}</li> ) ...

Attempting to obtain a score value from the input, but unsuccessful in doing so

Prior to posing this question, I had already managed to successfully retrieve the score value. Below is my original script: <script> $.ajax({ type: "POST", url: "https://example.com/paylist.php?acc=useraccount&json=1", ...

Customizing Body Color in CKEditor for Dynamic Designs

I am facing an issue with CKEditor that I am hoping to find a solution for. My scenario involves using a jQuery color picker to set the background color of a DIV element, which is then edited by the user in CKEditor. However, I have observed that it is not ...

Challenges with implementing speech recognition within a React component's state

I've encountered an issue with the React library react-speech-recognition. When trying to modify the newContent state within useEffect, it ends up printing as undefined. Additionally, I'm facing a similar problem when attempting to update the sta ...

tips for navigating through an AngularJS $resource instance

I am facing a frustrating issue that I need assistance with. The problem arises when I try to extract specific data from each element of the stock data fetched by my controller from Yahoo Stocks. Although the data is stored in $scope.stocks and can be disp ...

The Bootstrap modal simply fades away without ever making an appearance

My bootstrap modal is not displaying on desktop, only showing a faded screen. It works fine on mobile and tablet devices. Any ideas why this might be happening? Here is the code: <button type="button" class="btn btn-primary" data-to ...

Having trouble reaching a public method within an object passed to the @Input field of an Angular component

My configurator object declaration is as follows. export class Config { constructor(public index: number, public junk: string[] = []) { } public count() : number { return this.junk.length; } } After declaring it, I pass it into the input decorated fi ...

Leveraging $pristine in AngularJS for Form Validation

My form utilizes angular validation, but I want to disable it upon page load. Below is a simplified version of my form: <form ng-submit="vm.submit()" name="form" novalidate> <input class="form-control" ng-model="vm.userName" required /> ...

Modify the value of a CSS property through JavaScript

Hey there, I'm wondering how to change a CSS value of the document itself, rather than targeting a specific element. I've already looked into solutions like Change :hover CSS properties with JavaScript, but they all involve adding CSS rules. I a ...

This page is overwhelmed with an excessive number of active WebGL contexts, resulting in the inevitable loss of the oldest context. Brace yourselves, for the veteran context shall

I am experiencing a strange and perplexing error exclusively in the Safari browser. The cause remains elusive, leaving me puzzled. I have implemented AngularJS 1.3.x in my project. I wonder how I can identify which libraries might be triggering this myst ...

Ways to incorporate conditional checks prior to running class methods

Seeking input on handling async data retrieval elegantly. When initializing a class with asynchronous data, I have been following this approach: class SomeClass { // Disabling strictPropertyInitialization private someProperty: SomeType public asy ...

Tips for avoiding cursor sticking in css rotate transform in firefox?

I have a unique challenge in this code where I want the user to grab the black square and rotate it around the inner circle. Check out the code snippet here. While attempting to rotate the square, you might notice that the cursor sometimes gets stuck in ...