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.userLikes',
22  'app.travel',
23  'app.newTravel',
24  'app.notifications'
25]).
26config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
27  $locationProvider.hashPrefix('!');
28
29  //$routeProvider.otherwise({redirectTo: '/main'});
30  if((localStorage.getItem('cr_webapp_token')))
31    {
32      console.log(window.location.hash);
33      if((window.location.hash==='#!/login')||(window.location.hash==='#!/signup'))
34      {
35        window.location='#!/main';
36      }
37
38      $routeProvider.otherwise({redirectTo: '/main'});
39    }else{
40      if((window.location!=='#!/login')||(window.location!=='#!/signup'))
41      {
42        console.log('app, user no logged');
43
44        localStorage.removeItem('cr_webapp_token');
45        localStorage.removeItem('cr_webapp_userdata');
46        window.location='#!/login';
47        $routeProvider.otherwise({redirectTo: '/login'});
48      }
49    }
50}])
51.config(function(toastrConfig) {
52  angular.extend(toastrConfig, {
53    autoDismiss: false,
54    containerId: 'toast-container',
55    maxOpened: 0,
56    newestOnTop: true,
57    positionClass: 'toast-bottom-right',
58    preventDuplicates: false,
59    preventOpenDuplicates: false,
60    target: 'body'
61  });
62})
63.factory('httpInterceptor', function httpInterceptor () {
64  return {
65    request: function(config) {
66      return config;
67    },
68
69    requestError: function(config) {
70      return config;
71    },
72
73    response: function(res) {
74      return res;
75    },
76
77    responseError: function(res) {
78      return res;
79    }
80  };
81})
82.factory('api', function ($http) {
83	return {
84		init: function () {
85      $http.defaults.headers.common['X-Access-Token'] = localStorage.getItem('cr_webapp_token');
86      $http.defaults.headers.post['X-Access-Token'] = localStorage.getItem('cr_webapp_token');
87		}
88	};
89})
90.run(function (api) {
91	api.init();
92});