app.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. (function() {
  2. 'use strict';
  3. var HOST = 'http://cloudbudget.pavnay.fr/api';
  4. angular
  5. .module('cloudbudget', ['ngRoute', 'routes', 'ngCookies'])
  6. .constant('apiRoutes', {
  7. 'host': HOST,
  8. 'port': "80",
  9. 'login': HOST + '/users/login',
  10. 'register': HOST + '/users',
  11. 'unregister': HOST + '/users/'
  12. })
  13. .run(run);
  14. run.$inject = ['$rootScope', '$location', '$cookieStore', '$http', '$filter'];
  15. function run( $rootScope, $location, $cookieStore, $http, $filter) {
  16. $rootScope.globals = $cookieStore.get('globals') || {};
  17. if( $rootScope.globals.user && $rootScope.globals.user.token) {
  18. $http.defaults.headers.common['Authorization'] = 'JWT ' + $rootScope.globals.user.token;
  19. }
  20. $rootScope.$on('$locationChangeStart', function(event, next, current) {
  21. var restrictedPage = ['/login', '/register'].indexOf($location.path()) === -1;
  22. var loggedIn = $rootScope.globals.user;
  23. if( restrictedPage && !loggedIn ) {
  24. $location.path('/login');
  25. }
  26. });
  27. }
  28. })();