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

Is there a method for verifying the application signature in Ionic?

For the past 2 days, I've been on a quest to find information about app certificate validation libraries/functions in Ionic. After discovering SignatureCheck.java for Android (link: enter link description here), I wonder if there is a similar solution ...

Using Google App Script's pageToken to store attachments on Google Drive

Essentially, my goal is to save all attachments from received emails to a specific folder in Google Drive (mostly .PDF files). However, I've encountered a limitation with the search function which only allows me to retrieve up to 500 attached files. I ...

Synchronize JSON data with the Document Object Model (DOM

My current project is built using React, where I am rendering the page dynamically based on JSON data. The page consists of various component types, ranging from images to text content. Each component includes a delete option, allowing users to change im ...

Understanding the Mechanics of $route in AngularJS

I recently got asked in an interview about how the $route service works in AngularJS. According to my understanding, when a link or button is clicked, the $route service loads the template and then replaces the [ng-view] div/section with the HTML from the ...

In TypeScript, the function is failing to retrieve the complete array value

I'm facing an issue with a program that is supposed to piece together all the letters, but it's not functioning correctly. I've tried using the debugger, but it doesn't display any errors. Here's the code snippet: var phrase = [ &a ...

What is the best way to trigger an API call using AJAX whenever the page loads and at regular intervals using `setInterval()` function?

New to coding and seeking guidance: I have a basic AJAX feature in my project that triggers a GET API request every 3 seconds: <script> $(document).ready( function() { setInterval(function() { $.get(&apos ...

Sending a request to a JSON file using Ajax

I have 2 questions: 1. I am trying to run this file, but it is not giving any errors or showing results. Please help me identify the problem. 2. I added a dropdown menu in my HTML file, but I'm unsure how to use it to display a list of names. Any sugg ...

Transferring information between a pair of PHP functions via AJAX communications

Currently, I am tackling the challenge of user verification on our website. The process involves prompting users to input their credentials, click on the "send confirmation" button, receiving a code through SMS or messenger, entering the code in a field, a ...

Date Object Replacement Error: "this is not a valid Date object."

For my web application, I require a specific date value. I attempted to modify the Date prototype, but encountered an issue with Jquery Date recognizing it. InitDate = Date; InitDate.prototype = Date.prototype; Date = function () { if (arguments.leng ...

What is the best way to store all rows in a dynamically changing table with AngularJS?

I am facing an issue while trying to dynamically add rows for a variable that is a String array in my database. The problem is that it only saves the last value entered in the row instead of saving all of them in an array. Here is my current view code: &l ...

Encountering an Issue: Unable to Generate Line Chart with gRaphael Library

I'm currently working with the gRaphael JavaScript library to create a basic line graph. Here is the code I have implemented on my page: <script language="javascript" type="text/javascript"> var paper = Raphael(10, 50, 640, 480); paper.g.line ...

Creating a dynamically generated JavaScript array using the JSON format

I am in need of creating an array of JSON data. Here is an example: [ { "DataCategoryGroupId": "22222222-2222-2222-2222-222222222222", "AnswerOptionIds": [ "76e32546-0e26-4037-b253-823b21f6eefb", "10d02a3e-9f9f- ...

Pattern for matching alphabet and numerical characters, excluding whitespace

My AngularJS input text box should only accept alphabets and numbers, not special characters or spaces. I tried using "^[a-zA-Z0-9]+$", but it's not working as expected. The $error is only showing for special symbols, not spaces. Can someone please h ...

Using jQuery, compare the values of two elements and update the class based on the comparison outcome

My objective is to retrieve data from a database, place it into an <li> element, and then highlight the larger of the two values obtained from these HTML <li> elements. I have created a jsfiddle, but I am uncertain about how to use the addCla ...

Issue with Laravel 5.4: AJAX behaving unexpectedly and not returning errors as it should

After going through several tutorials on handling AJAX requests in Laravel, I'm still facing some issues. Each tutorial has its own approach... Finally, one method is not giving me a 500 error, but it's not displaying validation errors as expect ...

What is the best way to iterate through a JSON file?

Looking at my JSON file: { "stats": { "operators": { "recruit1": { "won": 100, "lost": 50, "timePlayed": 1000 }, "recruit2": { "won": 200, ...

Tips for acquiring an object to utilize in v-bind within a v-for loop

Consider this code snippet: <ol class="breadcrumb arr-right"> <li v-for="(url,name, index) in links" v-bind:class=" (index == (links.length -1)) ? 'breadcrumb-item active' : 'breadcrumb-item'"> <a v-bind:href ...

Encountering an issue when running the 'cypress open' command in Cypress

I am working with a Testing framework using node, cypress, mocha, mochawesome, and mochawesome-merge. You can check out the project on this github repo. https://i.sstatic.net/ItFpn.png In my package.json file, I have two scripts defined: "scripts": { ...

"Enhance Your Communication: Utilize setTimeout in Ajax

Hey there, I could really use some help with the setTimeout function in my code. No matter what I try, it just doesn't seem to work. I'm currently working on a chat system where I need to send and receive messages (testing by opening 2 browser ...

Assign a value to ng-model using JavaScript

Encountering an issue while working with JavaScript in AngularJS. There is a text field identified by id="longitude". <input type="text" data-ng-model="newwarehouse.longtitude" id="longitude"/> The value of this field is being set using JavaScript. ...