Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Learn MVC Using Angular Idle

DZone's Guide to

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.

Free Resource

Tips, tricks and tools for creating your own data-driven app, brought to you in partnership with Qlik.

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:

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.

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.

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:
mvc ,angular ,web dev ,login validation

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}