72702c656787e05bdbdb10ff92d5068afa5484cb
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / dlux-web / src / common / login / login.module.js
1 /*
2  * Copyright (c) 2014 Inocybe Technologies, and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 define(['angularAMD', 'jquery', 'common/authentification/auth.module', 'ocLazyLoad'], function (ng, $) {
10   var login = angular.module('app.common.login', ['app.common.auth', 'ui.router.state']);
11
12   login.config(function ($stateProvider, $httpProvider) {
13     $stateProvider
14       .state('login', {
15         url: '/login',
16         views: {
17           'mainContent@': {
18             templateUrl: 'src/common/login/login.tpl.html',
19             controller: 'LoginCtrl'
20           }
21         },
22         resolve: {
23           loadController: ['$ocLazyLoad', function ($ocLazyLoad) {
24             return $ocLazyLoad.load({
25               files: ['src/common/login/login.controller.js']
26             });
27           }]
28         }
29       });
30
31     $httpProvider.interceptors.push('NbInterceptor');
32   });
33
34   login.run(function ($rootScope, $location, Auth) {
35
36     // to avoid recursive loop
37     var publicPath = ['/login'];
38
39     var isPublicPath = function (route) {
40       var found = false;
41       $.each(publicPath, function (key, value) {
42         found = found || route.match('^' + value);
43       });
44       return found;
45     };
46
47     $rootScope.$on('$stateChangeStart', function () {
48       if (!isPublicPath($location.url()) && !Auth.isAuthed()) {
49         $location.path('/login');
50       }
51     });
52
53   });
54
55   return login;
56 });