Add seed code for sdnr app based on ONF Centennial
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / dlux / dlux-web / src / common / login / login.module.js
diff --git a/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/dlux-web/src/common/login/login.module.js b/sdnr/wireless-transport/code-Carbon-SR1/apps/dlux/dlux-web/src/common/login/login.module.js
new file mode 100644 (file)
index 0000000..72702c6
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2014 Inocybe Technologies, and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+define(['angularAMD', 'jquery', 'common/authentification/auth.module', 'ocLazyLoad'], function (ng, $) {
+  var login = angular.module('app.common.login', ['app.common.auth', 'ui.router.state']);
+
+  login.config(function ($stateProvider, $httpProvider) {
+    $stateProvider
+      .state('login', {
+        url: '/login',
+        views: {
+          'mainContent@': {
+            templateUrl: 'src/common/login/login.tpl.html',
+            controller: 'LoginCtrl'
+          }
+        },
+        resolve: {
+          loadController: ['$ocLazyLoad', function ($ocLazyLoad) {
+            return $ocLazyLoad.load({
+              files: ['src/common/login/login.controller.js']
+            });
+          }]
+        }
+      });
+
+    $httpProvider.interceptors.push('NbInterceptor');
+  });
+
+  login.run(function ($rootScope, $location, Auth) {
+
+    // to avoid recursive loop
+    var publicPath = ['/login'];
+
+    var isPublicPath = function (route) {
+      var found = false;
+      $.each(publicPath, function (key, value) {
+        found = found || route.match('^' + value);
+      });
+      return found;
+    };
+
+    $rootScope.$on('$stateChangeStart', function () {
+      if (!isPublicPath($location.url()) && !Auth.isAuthed()) {
+        $location.path('/login');
+      }
+    });
+
+  });
+
+  return login;
+});