client/app/app.js
author rougeronj
Fri, 19 Jun 2015 12:45:04 +0200
changeset 176 a8231241f50d
parent 172 ffdfe491869c
child 178 a3e1106b6f96
permissions -rw-r--r--
try to get first the context.token set up by the server. If it doesn't exist, try to get back the token in the local storage. Or get to the login page.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
(function(){
91
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
     2
    'use strict';
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
100
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
     4
    angular.module('ammico', [ 'ngRoute','ammicoHome', 'ammicoMyvisit', 'ammicoBooks', 'ammicoVisites', 'ammicoSlides', 'ammicoSlideshow', 'ammicoSearch', 'ammicoAuth', 'ammicoCommon', 'templates' ])
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
     5
    .config(function($routeProvider, $httpProvider) {
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 114
diff changeset
     6
        $httpProvider.interceptors.push(function($q) {
100
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
     7
            return {
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
     8
                responseError: function(rejection) {
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 114
diff changeset
     9
                    if (rejection.status === 401) {
100
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    10
                        localStorage.removeItem('token');
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    11
                    }
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    12
                    return $q.reject(rejection);
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    13
                }
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    14
            };
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    15
        });
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    16
        
91
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    17
        $routeProvider.
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    18
        when('/', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    19
            controller: 'homeCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    20
            templateUrl: 'home/home.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    21
            authRequired: false
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    22
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    23
        when('/my_visit', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    24
            controller: 'my_visitCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    25
            templateUrl: 'my_visit/my_visit.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    26
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    27
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    28
        when('/books', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    29
            controller: 'booksCtrl',
100
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    30
            templateUrl: 'books/books.html',
537d330ad7f0 use localstorage instead of sessionstorage
rougeronj
parents: 93
diff changeset
    31
            authRequired: true
91
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    32
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    33
        when('/books/:idBook', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    34
            controller: 'bookCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    35
            templateUrl: 'books/book.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    36
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    37
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    38
        when('/visites', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    39
            controller: 'visitesCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    40
            templateUrl: 'visites/visites.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    41
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    42
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    43
        when('/visites/:idVisit', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    44
            controller: 'visiteCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    45
            templateUrl: 'visites/visite.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    46
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    47
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    48
        when('/slide/:idSlide', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    49
            controller: 'slidesCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    50
            templateUrl: 'slides/slides.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    51
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    52
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    53
        when('/slideshow/', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    54
            controller: 'slideshowCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    55
            templateUrl: 'slideshow/slideshow.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    56
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    57
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    58
        when('/slideshow/:iSlide', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    59
            controller: 'slideshowCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    60
            templateUrl: 'slideshow/slideshow.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    61
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    62
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    63
        when('/search/:q', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    64
            controller: 'searchCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    65
            templateUrl: 'search/search.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    66
            authRequired: true
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    67
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    68
        when('/auth/:action', {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    69
            controller: 'authCtrl',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    70
            templateUrl: 'home/home.html',
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    71
            authRequired: false
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    72
        }).
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    73
        otherwise({
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    74
            redirectTo: '/'
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    75
        });
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    76
    })
114
e4ffc4e13a8e add a token field to the initialisation of the ammico app so the server can pass it and auto login with jwt
rougeronj
parents: 100
diff changeset
    77
    .run( function($rootScope, $location, $window, $http, authApi, context) {
91
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    78
        $rootScope.globals = {};
176
a8231241f50d try to get first the context.token set up by the server. If it doesn't exist, try to get back the token in the local storage. Or get to the login page.
rougeronj
parents: 172
diff changeset
    79
        if (context.token || localStorage.getItem('token')) {
91
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    80
            $rootScope.globals.userLogged = true;
176
a8231241f50d try to get first the context.token set up by the server. If it doesn't exist, try to get back the token in the local storage. Or get to the login page.
rougeronj
parents: 172
diff changeset
    81
            localStorage.setItem('token', (context.token || localStorage.getItem('token') ));
a8231241f50d try to get first the context.token set up by the server. If it doesn't exist, try to get back the token in the local storage. Or get to the login page.
rougeronj
parents: 172
diff changeset
    82
            $http.defaults.headers.common.Authorization = 'Token ' + (context.token || localStorage.getItem('token'));
91
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    83
        }
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    84
        $rootScope.$on( '$routeChangeStart', function(event, next) {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    85
            if (next.authRequired && !$rootScope.globals.userLogged) {
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    86
                $location.path( '/auth/login' );
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    87
            }
f7a844a9079e merge the changes of Camille
rougeronj
parents: 57
diff changeset
    88
        });
172
ffdfe491869c uncomment jshint and fix minor bug
rougeronj
parents: 114
diff changeset
    89
    });
0
cef349423167 add basic file org + client prototype
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
})();