arnaucube
arnaucube/commonroutesWebAppPublic Self-hosted repository
commonroutesWebApp/app.js
app.jsRaw
 1'use strict';
 2
 3//var urlapi = "http://localhost:3000/api/";
 4//var urlapi = "http://192.168.1.36:3000/api/";
 5var urlapi = "http://37.59.123.45:3000/api/";
 6
 7// Declare app level module which depends on views, and components
 8angular.module('webapp', [
 9  'ngRoute',
10  'ngMessages',
11  'angularBootstrapMaterial',
12  'toastr',
13  'app.navbar',
14  'app.signup',
15  'app.login',
16  'app.main',
17  'app.search',
18  'app.travels',
19  'app.users',
20  'app.user',
21  'app.travel',
22  'app.newTravel'
23]).
24config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
25  $locationProvider.hashPrefix('!');
26
27  //$routeProvider.otherwise({redirectTo: '/main'});
28  if((localStorage.getItem('cr_webapp_token')))
29    {
30      console.log(window.location.hash);
31      if((window.location.hash==='#!/login')||(window.location.hash==='#!/signup'))
32      {
33        window.location='#!/main';
34      }
35
36      $routeProvider.otherwise({redirectTo: '/main'});
37    }else{
38      if((window.location!=='#!/login')||(window.location!=='#!/signup'))
39      {
40        console.log('app, user no logged');
41
42        localStorage.removeItem('cr_webapp_token');
43        localStorage.removeItem('cr_webapp_userdata');
44        window.location='#!/login';
45        $routeProvider.otherwise({redirectTo: '/login'});
46      }
47    }
48}])
49.config(function(toastrConfig) {
50  angular.extend(toastrConfig, {
51    autoDismiss: false,
52    containerId: 'toast-container',
53    maxOpened: 0,
54    newestOnTop: true,
55    positionClass: 'toast-bottom-right',
56    preventDuplicates: false,
57    preventOpenDuplicates: false,
58    target: 'body'
59  });
60})
61.factory('httpInterceptor', function httpInterceptor () {
62  return {
63    request: function(config) {
64      return config;
65    },
66
67    requestError: function(config) {
68      return config;
69    },
70
71    response: function(res) {
72      return res;
73    },
74
75    responseError: function(res) {
76      return res;
77    }
78  };
79})
80.factory('api', function ($http) {
81	return {
82		init: function () {
83      $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('cr_webapp_token');
84      $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('cr_webapp_token');
85		}
86	};
87})
88.run(function (api) {
89	api.init();
90});