Introduction:
In this article, an approach is suggested for effective and secured user authentication in angular application.
Authentication as well as authorization are important aspects of any application. Application should not expose all of its data and functionality to just any user. Application users should authenticate themselves to see, access certain portions of application, or to perform certain action on application. To identify user and user’s permissions, we would require application user to be logged in. There is a difference in the way user management is implemented in different applications. Here one approach is suggested, for user authentication in angular application, using AngularUI Router as routing framework.
For large application, consisting of many modules, further comprising many tabs, sub-tabs etc. effective and secured user access management becomes a challenging task. Thus to simplify this, state based routing, provided by ui-router can be used. For using, approach suggested here, application structure must be considered, as depending upon the application structure, such as modules and tabs, nested views, multiple views, way in which views are rendered, user access permissions regarding views etc. many things vary from one application to other.
Example:
(Assigning state parameters to states)
// app.js app = angular.module(‘myUiRoutingApp’, ['ui.router']) .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) { $stateProvider .state(login, { url: "/login", templateUrl: "modules/main/views/login.html", params: { moduleID: "", tabID: "" } }) .state('user', { url: "/user", templateUrl: "modules/user/views/createuser.html", params: { moduleID: "UserModule", tabID: "User" } }) .state('user.profile', { url: "/userProfile'", templateUrl: "modules/user/views/userProfile.html", params: { moduleID: "UserModule", tabID: "User" } }) .state('rolesPermissions', { url: "/rolesPermissions", templateUrl: "modules/admin/views/rolesPermissions.html", params: { moduleID: "AdminModule", tabID: "rolesPermissions" } }) .state('assignUsers', { url: "/assignUsers", templateUrl: "modules/admin/views/'assignUsers'.html", params: { moduleID: "AdminModule", tabID: "UserAssignment" } }) } ]); |
(Note : In above example, it is assumed that, if a user has access to a particular tab then he/she also has access to its sub-tabs, and thus, have kept tabID same for the states ‘user’,’user.profile’. Also have not added sub-tabID for the same purpose.)
(Getting user info like access permissions after successful user login and preparing lists as below)
(Checking user is authenticated and is allowed to access the application data (URL) or not.)
$rootScope.$on('$stateChangeStart', |
Benefits:
Benefits of the URL/state based, user access privilege authentication approach are mentioned below:
It helps to achieve secured user access to application data and to handle impact of user roles and permissions to data considering changes in user management implementation.
It prevents unauthenticated users to access application data which they are not supposed to access or use. As it considers every URL change, thus covers almost max possible ways such as browser back/forward actions, keyboard based back/forward key press like events, cut-copy-paste of URLs etc. any of actions under current session.
It is easy to implement and has a flexible and maintainable approach.
State parameter does not directly provide any information related to users access and permissions. Thus, it plays safe.
Summary:
Based on application structure and requirements, we can perform URL based user access authentication. We can also use an angular factory/service, (e.g.myAuthService etc.) where we can store user info and authentication logic for the same.
If you have any suggestions regarding this article, please comment below. Thank you for reading. Hope this can help you perform, secured user authentication in Angular JS application in an easy manner.