DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • An Overview of Kubernetes Security Projects at KubeCon Europe 2023
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Redefining DevOps: The Transformative Power of Containerization

Trending

  • An Overview of Kubernetes Security Projects at KubeCon Europe 2023
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Redefining DevOps: The Transformative Power of Containerization
  1. DZone
  2. Coding
  3. Frameworks
  4. Learn MVC Using Angular Idle

Learn MVC Using Angular Idle

Have you ever been logged out of a web application due to inactivity? In this tutorial, we learn how to create this functionality using an Angular library.

Thiruppathi Rengasamy user avatar by
Thiruppathi Rengasamy
CORE ·
Jun. 22, 17 · Tutorial
Like (2)
Save
Tweet
Share
8.09K Views

Join the DZone community and get the full member experience.

Join For Free

This article demonstrates MVC using Angular Idle with a UI Bootstrap. This article will show you how to get your application to report if a user is idle.

Angular Idle

Angular Idle can use an Angular module to detect and respond to idle users. We can almost maintain the session on the client side.

Follow the steps given below and we can use an Angular Idle in AngularJS in MVC.

  • Create MVC project.
  • Configure Angular Idle.
  • Work with Angular Idle.

Create an MVC Project

Open Visual Studio 2015.


MVC

Go to New menu >Click New and then Project. Now, it will open a New Project Window.


MVC

You can select ASP.NET Web Application on Framework 4.6. Enter the name of the project in the Solution name text box, then click OK.

MVC

One more Window should appear. Select the MVC template in this popup and click Ok. Now, you can start.

Configure Angular Idle

We will download the idle plugin from the following sources:

  • Ng-Idle
  • UI-Bootstrap

Open the _Layout.cshtml and refer the.js file from downloaded folder to this view page:

<script src="~/Plugin/angular/angular.min.js"></script>  
<script src="~/Plugin/angular-ui-router/release/angular-ui-router.min.js"></script>  
<script src="~/Plugin/angular-idle/angular-idle.js"></script>  
<script src="~/Plugin/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>  

Link to My File

<script src="~/App/App.module.js"></script>  
<script src="~/App/App.config.js"></script>  
<script src="~/App/CarController.js"></script>  
<script src="~/App/LoginConttroller.js"></script> 

Angular Module

You will need to include the module as a dependency of your application.

var uiroute = angular  
.module('uiroute', ['ui.router', 'ngIdle', 'ui.bootstrap']) 

Angular Config

You should also set your options, using the KeepaliveProvider, IdleProvider in your Angular config file.

uiroute.config(['KeepaliveProvider', 'IdleProvider', function(KeepaliveProvider, IdleProvider) { 
    IdleProvider.idle(10);  
    IdleProvider.timeout(10);  
    KeepaliveProvider.interval(10);  
}]);  

Angular Controller

You can set what action will be performed on the user idle times. With the below code, I will be shown a warning message and be notified of the session logout.

function closeModals() {  
    if ($scope.warning) {  
        $scope.warning.close();  
        $scope.warning = null;  
    }  
    if ($scope.timedout) {  
        $scope.timedout.close();  
        $scope.timedout = null;  
    }  
}  
$scope.$on('IdleStart', function() {  
    closeModals();  
    $scope.warning = $uibModal.open({  
        templateUrl: 'warning-dialog.html',  
        windowClass: 'modal-danger'  
    });  
});  
$scope.$on('IdleEnd', function() {  
    closeModals();  
});  
$scope.$on('IdleTimeout', function() {  
    closeModals();  
    $scope.timedout = $uibModal.open({  
        templateUrl: 'timedout-dialog.html',  
        windowClass: 'modal-danger'  
    });  
});  

Angular Run

After configuring the angular.idle.js, you must initiate the function in your Angular Run function.

uiroute.run(['Idle', function(Idle) {  
    Idle.watch();  
}]);  

HTML Code

Set your HTML with the warning dialog and timeout dialog that is given below.

<script type="text/ng-template" id="warning-dialog.html">  
    <div class="modal-header small danger">  
        <h3>Your are Idle.</h3>  
    </div>  
    <div idle-countdown="countdown" ng-init="countdown=5" class="modal-body">  
        <uib-progressbar max="5" value="5" animate="false" class="progress-striped active">You'll be logged out in {{countdown}} second(s).</uib-progressbar>  
    </div>  
</script>  
<script type="text/ng-template" id="timedout-dialog.html">  
    <div class="modal-header small warning">  
        <h3>You have Timed Out!</h3>  
    </div>  
    <div class="modal-body">  
        <h2> You were idle too long. you'll be reset. </h2>  
    </div>  
    <div class="modal-footer"> <button class="btn btn-warning" ui-sref="login">Logout</button> </div>  
</script>  

This code should configure your main Angular Controller and where we need to go to show this idle screen.

Angular Route

uiroute.config(function($stateProvider, $urlRouterProvider) {  
            $urlRouterProvider.otherwise('/login');  
            $stateProvider  
                // State managing  
                .state('login', {  
                    url: '/login',  
                    templateUrl: '/App/Test/login.html',  
                    controller: 'LoginController'  
                })  
                //Manager Role  
                .state('manager', {  
                    url: '/manager',  
                    templateUrl: '/App/Manager/home.html'  
                }).state('manager.list', {  
                    url: '/list',  
                    templateUrl: '/App/Test/dataList.html',  
                    controller: 'CarController'  
                });  

Here, I have used Angular UI route to navigate to the URL.

If you have any questions about this configuration, visit the article links given below.

  • Learn Basics Of MVC Using AngularJS
  • Learn MVC Using Angular UI-Route
  • Learn MVC Using Angular Role Based Login
  • Learn MVC Using Angular Datatable
  • Learn MVC Using Angular Crystal Report
  • Learn MVC Using Angular Pie Chart
  • Learn MVC Using Angular Flot Chart
  • Learn MVC Using Angular xEditable

Run the Application. Now, it should appear in the browser and you should see the results.

Output 1

MVC

You need to refer to the login controller for login username and password.

if ($scope.UserName.toUpperCase() == 'MANAGER') {  
    if ($scope.Password == '1') {  
        $state.go('manager')  
    }  
}  

Output 2

After the Login Manager, the home page will appear, as shown below.

MVC

Output 3

Stay idle for 5 seconds. Now, click or touch the screen to reset your idle state.

MVC

Output 4

Over your idle minutes, it will ask you to Logout.

MVC

Output 5

Suppose a user is busy with some other tab in the browser. You can set an idle countdown and sessions expiration information will load on top of the browser tab.

MVC
MVC

In this article, we have taken a look at MVC using Angular Idle. If you have any queries, please tell me in the comments section. Your comments are very valuable.

AngularJS IDLE (Python)

Published at DZone with permission of Thiruppathi Rengasamy, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • An Overview of Kubernetes Security Projects at KubeCon Europe 2023
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  • Tomorrow’s Cloud Today: Unpacking the Future of Cloud Computing
  • Redefining DevOps: The Transformative Power of Containerization

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: