Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ionic Starter App
- // angular.module is a global place for creating, registering and retrieving Angular modules
- // 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
- // the 2nd parameter is an array of 'requires'
- angular.module('starter', ['ionic', 'ngCordova','starter.controllers', 'starter.services','punctis.services','angular-storage','auth0','angular-jwt'])
- .config(function($stateProvider, $urlRouterProvider, $httpProvider, authProvider, jwtInterceptorProvider) {
- // Ionic uses AngularUI Router which uses the concept of states
- // Learn more here: https://github.com/angular-ui/ui-router
- // Set up the various states which the app can be in.
- // Each state's controller can be found in controllers.js
- $stateProvider
- // This is the state where you'll show the login
- .state('login', {
- url: '/login',
- templateUrl: 'templates/login.html',
- controller: 'LoginCtrl',
- })
- .state('welcome', {
- url: "/welcome",
- abstract: true,
- templateUrl: "templates/abstracts/welcome.html"
- })
- .state('mainmenu', {
- url: "/mainmenu",
- abstract: true,
- templateUrl: "templates/rightmenu/abstracts/main-menu.html",
- controller: "MenuCtrl"
- })
- .state('mainmenu.articles', {
- url: "/articles",
- abstract: true,
- views: {
- 'menuContent' :{
- templateUrl: 'templates/articles/abstracts/articles.html'
- }
- }
- })
- .state('mainmenu.articles.list', {
- url: "/list",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/articles/articles-list.html',
- controller: 'ArticlesListCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.articles.article', {
- url: "/article/:articleId",
- abstract: true,
- views: {
- 'details-tab' :{
- templateUrl: 'templates/articles/abstracts/article.html'
- }
- }
- })
- .state('mainmenu.articles.article.info', {
- url: "/info",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/articles/article-detail.html',
- controller: 'ArticleInfoCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.articles.article.giacenza', {
- url: "/giacenza",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/articles/article-giacenza.html',
- controller: 'ArticleGiacenzaCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.articles.article.movimentimagazzino', {
- url: "/movimentimagazzino",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/articles/article-movimenti.html',
- controller: 'ArticleMovimentiCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.customers', {
- url: "/customers",
- abstract: true,
- views: {
- 'menuContent' :{
- templateUrl: 'templates/customers/abstracts/customers.html'
- }
- }
- })
- .state('mainmenu.customers.list', {
- url: "/list",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/customers/customers-list.html',
- controller: 'CustomersListCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.customers.customer', {
- url: "/customer/:customerId",
- abstract: true,
- views: {
- 'details-tab' :{
- templateUrl: 'templates/customers/abstracts/customer.html'
- }
- }
- })
- .state('mainmenu.customers.customer.info', {
- url: "/info",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/customers/customer-detail.html',
- controller: 'CustomerInfoCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.customers.customer.partitario', {
- url: "/partitario",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/customers/customer-partitario.html',
- controller: 'CustomerPartitarioCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.providers', {
- url: "/providers",
- abstract: true,
- views: {
- 'menuContent' :{
- templateUrl: 'templates/providers/abstracts/providers.html'
- }
- }
- })
- .state('mainmenu.providers.list', {
- url: "/list",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/providers/providers-list.html',
- controller: 'ProvidersListCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.providers.provider', {
- url: "/provider/:providerId",
- abstract: true,
- views: {
- 'details-tab' :{
- templateUrl: 'templates/providers/abstracts/provider.html'
- }
- }
- })
- .state('mainmenu.providers.provider.info', {
- url: "/info",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/providers/provider-detail.html',
- controller: 'ProviderInfoCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.providers.provider.partitario', {
- url: "/partitario",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/providers/provider-partitario.html',
- controller: 'ProviderPartitarioCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.contacts', {
- url: "/contacts",
- abstract: true,
- views: {
- 'menuContent' :{
- templateUrl: 'templates/contacts/abstracts/contacts.html'
- }
- }
- })
- .state('mainmenu.contacts.list', {
- url: "/list",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/contacts/contacts-list.html',
- controller: 'ContactsListCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.contacts.contact', {
- url: "/contact/:contactId",
- abstract: true,
- views: {
- 'details-tab' :{
- templateUrl: 'templates/contacts/abstracts/contact.html'
- }
- }
- })
- .state('mainmenu.contacts.contact.info', {
- url: "/info",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/contacts/contact-detail.html',
- controller: 'ContactInfoCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.agendas', {
- url: "/agendas",
- abstract: true,
- views: {
- 'menuContent' :{
- templateUrl: 'templates/agenda/abstracts/agendas.html'
- }
- }
- })
- .state('mainmenu.agendas.list', {
- url: "/list",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/agendas-list.html',
- controller: 'AgendasListCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.agendas.agenda', {
- url: "/agenda",
- abstract: true,
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/abstracts/agenda.html'
- }
- }
- })
- .state('mainmenu.agendas.agenda.list', {
- url: "/:agendaId/list/:year/:month/:day",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/agenda-list.html',
- controller: 'AgendaListCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.agendas.agenda.add', {
- url: "/:agendaId/add/:year/:month/:day/:orario",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/agenda-add.html',
- controller: 'AgendaAddCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.agendas.agenda.event', {
- url: "/:agendaId/event/:eventId",
- abstract: true,
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/abstracts/event.html'
- }
- }
- })
- .state('mainmenu.agendas.agenda.event.info', {
- url: "/info",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/event-detail.html',
- controller: 'EventInfoCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- .state('mainmenu.agendas.agenda.event.edit', {
- url: "/edit",
- views: {
- 'details-tab' :{
- templateUrl: 'templates/agenda/event-edit.html',
- controller: 'EventEditCtrl'
- }
- },
- data: {
- // This tells Auth0 that this state requires the user to be logged in.
- // If the user isn't logged in and he tries to access this state
- // he'll be redirected to the login page
- requiresLogin: true
- }
- })
- authProvider.init({
- domain: 'progettoautomazione.auth0.com',
- clientID: 'zl8xdZEVD5c3L9fVY7VFxPpJfqmJDV0t',
- loginState: 'login'
- });
- // if none of the above states are matched, use this as the fallback
- $urlRouterProvider.otherwise('/login');
- jwtInterceptorProvider.tokenGetter = function(store, jwtHelper, auth) {
- var idToken = store.get('token');
- var refreshToken = store.get('refreshToken');
- // If no token return null
- if (!idToken || !refreshToken) {
- return null;
- }
- // If token is expired, get a new one
- if (jwtHelper.isTokenExpired(idToken)) {
- return auth.refreshIdToken(refreshToken).then(function(idToken) {
- store.set('token', idToken);
- return idToken;
- });
- } else {
- return idToken;
- }
- }
- $httpProvider.interceptors.push('jwtInterceptor');
- })
- .run(function($ionicPlatform,$rootScope, auth, store, jwtHelper, $location) {
- $ionicPlatform.ready(function() {
- //initPushwoosh();
- //document.addEventListener("deviceready", initPushwoosh, true);
- // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
- // for form inputs)
- if(window.cordova && window.cordova.plugins.Keyboard) {
- cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
- }
- if(window.StatusBar) {
- StatusBar.styleDefault();
- }
- // This hooks al auth events to check everything as soon as the app starts
- auth.hookEvents();
- });
- $rootScope.$on('$locationChangeStart', function() {
- if (!auth.isAuthenticated) {
- console.log("Non autenticato");
- alert("Non auth");
- //store.set('token', "");
- //store.set('refreshToken', "");
- var token = store.get('token');
- var refreshToken = store.get('refreshToken');
- if (token) {
- console.log(token);
- if (!jwtHelper.isTokenExpired(token)) {
- console.log("Token valido");
- alert("Token valido");
- auth.authenticate(store.get('profile'), token);
- } else {
- alert(refreshToken);
- if (refreshToken) {
- console.log("Token scaduto");
- alert("Token NON valido");
- auth.refreshIdToken(refreshToken).then(function(idToken) {
- console.log(idToken);
- alert(idToken);
- store.set('token', idToken);
- auth.authenticate(store.get('profile'), idToken);
- return idToken;
- });
- }else {
- $location.path('/login');
- }
- }
- }
- }
- });
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement