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