client/app/app.js
author rougeronj
Thu, 04 Jun 2015 20:10:38 +0200
changeset 114 e4ffc4e13a8e
parent 100 537d330ad7f0
child 172 ffdfe491869c
permissions -rw-r--r--
add a token field to the initialisation of the ammico app so the server can pass it and auto login with jwt

(function(){
    'use strict';

    angular.module('ammico', [ 'ngRoute','ammicoHome', 'ammicoMyvisit', 'ammicoBooks', 'ammicoVisites', 'ammicoSlides', 'ammicoSlideshow', 'ammicoSearch', 'ammicoAuth', 'ammicoCommon', 'templates' ])
    .config(function($routeProvider, $httpProvider) {
        $httpProvider.interceptors.push(function($q, $location) {
            return {
                responseError: function(rejection) {
                    if (rejection.status == 401) {
                        localStorage.removeItem('token');
                    }
                    return $q.reject(rejection);
                }
            };
        });
        
        $routeProvider.
        when('/', {
            controller: 'homeCtrl',
            templateUrl: 'home/home.html',
            authRequired: false
        }).
        when('/my_visit', {
            controller: 'my_visitCtrl',
            templateUrl: 'my_visit/my_visit.html',
            authRequired: true
        }).
        when('/books', {
            controller: 'booksCtrl',
            templateUrl: 'books/books.html',
            authRequired: true
        }).
        when('/books/:idBook', {
            controller: 'bookCtrl',
            templateUrl: 'books/book.html',
            authRequired: true
        }).
        when('/visites', {
            controller: 'visitesCtrl',
            templateUrl: 'visites/visites.html',
            authRequired: true
        }).
        when('/visites/:idVisit', {
            controller: 'visiteCtrl',
            templateUrl: 'visites/visite.html',
            authRequired: true
        }).
        when('/slide/:idSlide', {
            controller: 'slidesCtrl',
            templateUrl: 'slides/slides.html',
            authRequired: true
        }).
        when('/slideshow/', {
            controller: 'slideshowCtrl',
            templateUrl: 'slideshow/slideshow.html',
            authRequired: true
        }).
        when('/slideshow/:iSlide', {
            controller: 'slideshowCtrl',
            templateUrl: 'slideshow/slideshow.html',
            authRequired: true
        }).
        when('/search/:q', {
            controller: 'searchCtrl',
            templateUrl: 'search/search.html',
            authRequired: true
        }).
        when('/auth/:action', {
            controller: 'authCtrl',
            templateUrl: 'home/home.html',
            authRequired: false
        }).
        otherwise({
            redirectTo: '/'
        });
    })
    .run( function($rootScope, $location, $window, $http, authApi, context) {
        $rootScope.globals = {};
        if (localStorage.getItem('token') || context['token']) {
            $rootScope.globals.userLogged = true;
            localStorage.setItem('token', (localStorage.getItem('token') || context['token']));
            $http.defaults.headers.common.Authorization = 'Token ' + (localStorage.getItem('token') || context['token']);
        }
        $rootScope.$on( '$routeChangeStart', function(event, next) {
            if (next.authRequired && !$rootScope.globals.userLogged) {
                $location.path( '/auth/login' );
            }
        });
    })
})();