Advertisement
Guest User

Untitled

a guest
Dec 11th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Ionic Starter App
  2.  
  3. // angular.module is a global place for creating, registering and retrieving Angular modules
  4. // 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
  5. // the 2nd parameter is an array of 'requires'
  6.  
  7. angular.module('starter', ['ionic', 'ngCordova','starter.controllers', 'starter.services','punctis.services','angular-storage','auth0','angular-jwt'])
  8.  
  9. .config(function($stateProvider, $urlRouterProvider, $httpProvider, authProvider, jwtInterceptorProvider) {
  10.  
  11.  
  12.   // Ionic uses AngularUI Router which uses the concept of states
  13.   // Learn more here: https://github.com/angular-ui/ui-router
  14.   // Set up the various states which the app can be in.
  15.   // Each state's controller can be found in controllers.js
  16.   $stateProvider
  17.     // This is the state where you'll show the login
  18.     .state('login', {
  19.       url: '/login',
  20.       templateUrl: 'templates/login.html',
  21.       controller: 'LoginCtrl',
  22.     })
  23.  
  24.     .state('welcome', {
  25.       url: "/welcome",
  26.       abstract: true,
  27.       templateUrl: "templates/abstracts/welcome.html"
  28.     })
  29.      
  30.     .state('mainmenu', {
  31.       url: "/mainmenu",
  32.       abstract: true,
  33.       templateUrl: "templates/rightmenu/abstracts/main-menu.html",
  34.       controller: "MenuCtrl"
  35.     })
  36.       .state('mainmenu.articles', {
  37.         url: "/articles",
  38.         abstract: true,
  39.         views: {
  40.           'menuContent' :{
  41.             templateUrl: 'templates/articles/abstracts/articles.html'
  42.           }
  43.         }
  44.       })
  45.         .state('mainmenu.articles.list', {
  46.           url: "/list",
  47.           views: {
  48.             'details-tab' :{
  49.               templateUrl: 'templates/articles/articles-list.html',
  50.               controller: 'ArticlesListCtrl'
  51.             }
  52.           },
  53.           data: {
  54.             // This tells Auth0 that this state requires the user to be logged in.
  55.             // If the user isn't logged in and he tries to access this state
  56.             // he'll be redirected to the login page
  57.             requiresLogin: true
  58.           }
  59.         })
  60.       .state('mainmenu.articles.article', {
  61.         url: "/article/:articleId",
  62.         abstract: true,
  63.         views: {
  64.           'details-tab' :{
  65.             templateUrl: 'templates/articles/abstracts/article.html'
  66.           }
  67.         }
  68.       })
  69.         .state('mainmenu.articles.article.info', {
  70.           url: "/info",
  71.           views: {
  72.             'details-tab' :{
  73.               templateUrl: 'templates/articles/article-detail.html',
  74.               controller: 'ArticleInfoCtrl'
  75.             }
  76.           },
  77.           data: {
  78.             // This tells Auth0 that this state requires the user to be logged in.
  79.             // If the user isn't logged in and he tries to access this state
  80.             // he'll be redirected to the login page
  81.             requiresLogin: true
  82.           }
  83.         })
  84.         .state('mainmenu.articles.article.giacenza', {
  85.           url: "/giacenza",
  86.           views: {
  87.             'details-tab' :{
  88.               templateUrl: 'templates/articles/article-giacenza.html',
  89.               controller: 'ArticleGiacenzaCtrl'
  90.             }
  91.           },
  92.           data: {
  93.             // This tells Auth0 that this state requires the user to be logged in.
  94.             // If the user isn't logged in and he tries to access this state
  95.             // he'll be redirected to the login page
  96.             requiresLogin: true
  97.           }
  98.         })
  99.         .state('mainmenu.articles.article.movimentimagazzino', {
  100.           url: "/movimentimagazzino",
  101.           views: {
  102.             'details-tab' :{
  103.               templateUrl: 'templates/articles/article-movimenti.html',
  104.               controller: 'ArticleMovimentiCtrl'
  105.             }
  106.           },
  107.           data: {
  108.             // This tells Auth0 that this state requires the user to be logged in.
  109.             // If the user isn't logged in and he tries to access this state
  110.             // he'll be redirected to the login page
  111.             requiresLogin: true
  112.           }
  113.         })
  114.       .state('mainmenu.customers', {
  115.         url: "/customers",
  116.         abstract: true,
  117.         views: {
  118.           'menuContent' :{
  119.             templateUrl: 'templates/customers/abstracts/customers.html'
  120.           }
  121.         }
  122.       })
  123.         .state('mainmenu.customers.list', {
  124.           url: "/list",
  125.           views: {
  126.             'details-tab' :{
  127.               templateUrl: 'templates/customers/customers-list.html',
  128.               controller: 'CustomersListCtrl'
  129.             }
  130.           },
  131.           data: {
  132.             // This tells Auth0 that this state requires the user to be logged in.
  133.             // If the user isn't logged in and he tries to access this state
  134.             // he'll be redirected to the login page
  135.             requiresLogin: true
  136.           }
  137.         })
  138.       .state('mainmenu.customers.customer', {
  139.         url: "/customer/:customerId",
  140.         abstract: true,
  141.         views: {
  142.           'details-tab' :{
  143.             templateUrl: 'templates/customers/abstracts/customer.html'
  144.           }
  145.         }
  146.       })
  147.         .state('mainmenu.customers.customer.info', {
  148.           url: "/info",
  149.           views: {
  150.             'details-tab' :{
  151.               templateUrl: 'templates/customers/customer-detail.html',
  152.               controller: 'CustomerInfoCtrl'
  153.             }
  154.           },
  155.           data: {
  156.             // This tells Auth0 that this state requires the user to be logged in.
  157.             // If the user isn't logged in and he tries to access this state
  158.             // he'll be redirected to the login page
  159.             requiresLogin: true
  160.           }
  161.         })
  162.         .state('mainmenu.customers.customer.partitario', {
  163.           url: "/partitario",
  164.           views: {
  165.             'details-tab' :{
  166.               templateUrl: 'templates/customers/customer-partitario.html',
  167.               controller: 'CustomerPartitarioCtrl'
  168.             }
  169.           },
  170.           data: {
  171.             // This tells Auth0 that this state requires the user to be logged in.
  172.             // If the user isn't logged in and he tries to access this state
  173.             // he'll be redirected to the login page
  174.             requiresLogin: true
  175.           }
  176.         })
  177.       .state('mainmenu.providers', {
  178.         url: "/providers",
  179.         abstract: true,
  180.         views: {
  181.           'menuContent' :{
  182.             templateUrl: 'templates/providers/abstracts/providers.html'
  183.           }
  184.         }
  185.       })
  186.         .state('mainmenu.providers.list', {
  187.           url: "/list",
  188.           views: {
  189.             'details-tab' :{
  190.               templateUrl: 'templates/providers/providers-list.html',
  191.               controller: 'ProvidersListCtrl'
  192.             }
  193.           },
  194.           data: {
  195.             // This tells Auth0 that this state requires the user to be logged in.
  196.             // If the user isn't logged in and he tries to access this state
  197.             // he'll be redirected to the login page
  198.             requiresLogin: true
  199.           }
  200.         })
  201.       .state('mainmenu.providers.provider', {
  202.         url: "/provider/:providerId",
  203.         abstract: true,
  204.         views: {
  205.           'details-tab' :{
  206.             templateUrl: 'templates/providers/abstracts/provider.html'
  207.           }
  208.         }
  209.       })
  210.         .state('mainmenu.providers.provider.info', {
  211.           url: "/info",
  212.           views: {
  213.             'details-tab' :{
  214.               templateUrl: 'templates/providers/provider-detail.html',
  215.               controller: 'ProviderInfoCtrl'
  216.             }
  217.           },
  218.           data: {
  219.             // This tells Auth0 that this state requires the user to be logged in.
  220.             // If the user isn't logged in and he tries to access this state
  221.             // he'll be redirected to the login page
  222.             requiresLogin: true
  223.           }
  224.         })
  225.         .state('mainmenu.providers.provider.partitario', {
  226.           url: "/partitario",
  227.           views: {
  228.             'details-tab' :{
  229.               templateUrl: 'templates/providers/provider-partitario.html',
  230.               controller: 'ProviderPartitarioCtrl'
  231.             }
  232.           },
  233.           data: {
  234.             // This tells Auth0 that this state requires the user to be logged in.
  235.             // If the user isn't logged in and he tries to access this state
  236.             // he'll be redirected to the login page
  237.             requiresLogin: true
  238.           }
  239.         })
  240.       .state('mainmenu.contacts', {
  241.         url: "/contacts",
  242.         abstract: true,
  243.         views: {
  244.           'menuContent' :{
  245.             templateUrl: 'templates/contacts/abstracts/contacts.html'
  246.           }
  247.         }
  248.       })
  249.         .state('mainmenu.contacts.list', {
  250.           url: "/list",
  251.           views: {
  252.             'details-tab' :{
  253.               templateUrl: 'templates/contacts/contacts-list.html',
  254.               controller: 'ContactsListCtrl'
  255.             }
  256.           },
  257.           data: {
  258.             // This tells Auth0 that this state requires the user to be logged in.
  259.             // If the user isn't logged in and he tries to access this state
  260.             // he'll be redirected to the login page
  261.             requiresLogin: true
  262.           }
  263.         })
  264.       .state('mainmenu.contacts.contact', {
  265.         url: "/contact/:contactId",
  266.         abstract: true,
  267.         views: {
  268.           'details-tab' :{
  269.             templateUrl: 'templates/contacts/abstracts/contact.html'
  270.           }
  271.         }
  272.       })
  273.         .state('mainmenu.contacts.contact.info', {
  274.           url: "/info",
  275.           views: {
  276.             'details-tab' :{
  277.               templateUrl: 'templates/contacts/contact-detail.html',
  278.               controller: 'ContactInfoCtrl'
  279.             }
  280.           },
  281.           data: {
  282.             // This tells Auth0 that this state requires the user to be logged in.
  283.             // If the user isn't logged in and he tries to access this state
  284.             // he'll be redirected to the login page
  285.             requiresLogin: true
  286.           }
  287.         })
  288.     .state('mainmenu.agendas', {
  289.       url: "/agendas",
  290.       abstract: true,
  291.       views: {
  292.         'menuContent' :{
  293.           templateUrl: 'templates/agenda/abstracts/agendas.html'
  294.         }
  295.       }
  296.     })
  297.       .state('mainmenu.agendas.list', {
  298.         url: "/list",
  299.         views: {
  300.           'details-tab' :{
  301.             templateUrl: 'templates/agenda/agendas-list.html',
  302.             controller: 'AgendasListCtrl'
  303.           }
  304.         },
  305.           data: {
  306.             // This tells Auth0 that this state requires the user to be logged in.
  307.             // If the user isn't logged in and he tries to access this state
  308.             // he'll be redirected to the login page
  309.             requiresLogin: true
  310.           }
  311.       })
  312.       .state('mainmenu.agendas.agenda', {
  313.         url: "/agenda",
  314.         abstract: true,
  315.         views: {
  316.           'details-tab' :{
  317.             templateUrl: 'templates/agenda/abstracts/agenda.html'
  318.           }
  319.         }
  320.       })
  321.         .state('mainmenu.agendas.agenda.list', {
  322.           url: "/:agendaId/list/:year/:month/:day",
  323.           views: {
  324.             'details-tab' :{
  325.               templateUrl: 'templates/agenda/agenda-list.html',
  326.               controller: 'AgendaListCtrl'
  327.             }
  328.           },
  329.           data: {
  330.             // This tells Auth0 that this state requires the user to be logged in.
  331.             // If the user isn't logged in and he tries to access this state
  332.             // he'll be redirected to the login page
  333.             requiresLogin: true
  334.           }
  335.         })
  336.         .state('mainmenu.agendas.agenda.add', {
  337.           url: "/:agendaId/add/:year/:month/:day/:orario",
  338.           views: {
  339.             'details-tab' :{
  340.               templateUrl: 'templates/agenda/agenda-add.html',
  341.               controller: 'AgendaAddCtrl'
  342.             }
  343.           },
  344.           data: {
  345.             // This tells Auth0 that this state requires the user to be logged in.
  346.             // If the user isn't logged in and he tries to access this state
  347.             // he'll be redirected to the login page
  348.             requiresLogin: true
  349.           }
  350.         })
  351.         .state('mainmenu.agendas.agenda.event', {
  352.         url: "/:agendaId/event/:eventId",
  353.         abstract: true,
  354.         views: {
  355.           'details-tab' :{
  356.             templateUrl: 'templates/agenda/abstracts/event.html'
  357.           }
  358.         }
  359.       })
  360.         .state('mainmenu.agendas.agenda.event.info', {
  361.           url: "/info",
  362.           views: {
  363.             'details-tab' :{
  364.               templateUrl: 'templates/agenda/event-detail.html',
  365.               controller: 'EventInfoCtrl'
  366.             }
  367.           },
  368.           data: {
  369.             // This tells Auth0 that this state requires the user to be logged in.
  370.             // If the user isn't logged in and he tries to access this state
  371.             // he'll be redirected to the login page
  372.             requiresLogin: true
  373.           }
  374.         })
  375.         .state('mainmenu.agendas.agenda.event.edit', {
  376.           url: "/edit",
  377.           views: {
  378.             'details-tab' :{
  379.               templateUrl: 'templates/agenda/event-edit.html',
  380.               controller: 'EventEditCtrl'
  381.             }
  382.           },
  383.           data: {
  384.             // This tells Auth0 that this state requires the user to be logged in.
  385.             // If the user isn't logged in and he tries to access this state
  386.             // he'll be redirected to the login page
  387.             requiresLogin: true
  388.           }
  389.         })
  390.  
  391.         authProvider.init({
  392.           domain: 'progettoautomazione.auth0.com',
  393.           clientID: 'zl8xdZEVD5c3L9fVY7VFxPpJfqmJDV0t',
  394.           loginState: 'login'
  395.         });
  396.    
  397.  
  398.   // if none of the above states are matched, use this as the fallback
  399.   $urlRouterProvider.otherwise('/login');
  400.  
  401.   jwtInterceptorProvider.tokenGetter = function(store, jwtHelper, auth) {
  402.     var idToken = store.get('token');
  403.     var refreshToken = store.get('refreshToken');
  404.     // If no token return null
  405.     if (!idToken || !refreshToken) {
  406.       return null;
  407.     }
  408.     // If token is expired, get a new one
  409.     if (jwtHelper.isTokenExpired(idToken)) {
  410.       return auth.refreshIdToken(refreshToken).then(function(idToken) {
  411.         store.set('token', idToken);
  412.         return idToken;
  413.       });
  414.     } else {
  415.       return idToken;
  416.     }
  417.   }
  418.  
  419.   $httpProvider.interceptors.push('jwtInterceptor');
  420.  
  421. })
  422. .run(function($ionicPlatform,$rootScope, auth, store, jwtHelper, $location) {
  423.   $ionicPlatform.ready(function() {
  424.     //initPushwoosh();
  425.     //document.addEventListener("deviceready", initPushwoosh, true);
  426.     // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
  427.     // for form inputs)
  428.     if(window.cordova && window.cordova.plugins.Keyboard) {
  429.       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  430.     }
  431.     if(window.StatusBar) {
  432.       StatusBar.styleDefault();
  433.     }
  434.     // This hooks al auth events to check everything as soon as the app starts
  435.     auth.hookEvents();
  436.   });
  437.   $rootScope.$on('$locationChangeStart', function() {
  438.     if (!auth.isAuthenticated) {
  439.       console.log("Non autenticato");
  440.       alert("Non auth");
  441.       //store.set('token', "");
  442.       //store.set('refreshToken', "");
  443.       var token = store.get('token');
  444.       var refreshToken = store.get('refreshToken');
  445.       if (token) {
  446.         console.log(token);
  447.         if (!jwtHelper.isTokenExpired(token)) {
  448.           console.log("Token valido");
  449.           alert("Token valido");
  450.           auth.authenticate(store.get('profile'), token);
  451.         } else {
  452.           alert(refreshToken);
  453.           if (refreshToken) {
  454.             console.log("Token scaduto");
  455.             alert("Token NON valido");
  456.             auth.refreshIdToken(refreshToken).then(function(idToken) {
  457.               console.log(idToken);
  458.               alert(idToken);
  459.               store.set('token', idToken);
  460.               auth.authenticate(store.get('profile'), idToken);
  461.               return idToken;
  462.             });
  463.           }else {
  464.             $location.path('/login');
  465.           }
  466.         }
  467.       }
  468.     }
  469.   });
  470. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement