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