use localstorage instead of sessionstorage
authorrougeronj
Wed, 27 May 2015 19:03:08 +0200
changeset 100 537d330ad7f0
parent 99 c7c424e9eed5
child 101 4dfeab3852da
use localstorage instead of sessionstorage
client/app/app.js
client/app/auth/auth_controller.js
client/gulpfile.js
--- a/client/app/app.js	Wed May 27 19:02:43 2015 +0200
+++ b/client/app/app.js	Wed May 27 19:03:08 2015 +0200
@@ -1,8 +1,19 @@
 (function(){
     'use strict';
 
-    angular.module('ammico', [ 'ngRoute','ammicoHome', 'ammicoMyvisit', 'ammicoBooks', 'ammicoVisites', 'ammicoSlides', 'ammicoSlideshow', 'ammicoSearch', 'ammicoAuth', 'ammicoModels', 'templates' ])
-    .config(function($routeProvider) {
+    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',
@@ -16,7 +27,8 @@
         }).
         when('/books', {
             controller: 'booksCtrl',
-            templateUrl: 'books/books.html'
+            templateUrl: 'books/books.html',
+            authRequired: true
         }).
         when('/books/:idBook', {
             controller: 'bookCtrl',
@@ -62,11 +74,11 @@
             redirectTo: '/'
         });
     })
-    .run( function($rootScope, $location, $window, $http) {
+    .run( function($rootScope, $location, $window, $http, authApi) {
         $rootScope.globals = {};
-        if ($window.sessionStorage.token) {
+        if (localStorage.getItem('token')) {
             $rootScope.globals.userLogged = true;
-            $http.defaults.headers.common.Authorization = 'Token ' + $window.sessionStorage.token;
+            $http.defaults.headers.common.Authorization = 'Token ' + localStorage.getItem('token');
         }
         $rootScope.$on( '$routeChangeStart', function(event, next) {
             if (next.authRequired && !$rootScope.globals.userLogged) {
@@ -74,57 +86,4 @@
             }
         });
     })
-    .service('searchApi', function($resource, context) {
-        this.searchResource = function(params){
-            return $resource(context.urls.searchUrl,  
-                    {
-                callback: 'JSON_CALLBACK'
-                    },
-                    {
-                        getJsonp: {
-                            method: 'JSONP',
-                            params: params,
-                            isArray: false,
-                            transformResponse: function(data){
-                                // Transform meta list into meta dict
-                                var nb = data.hits.length;
-                                for(var i=0;i<nb;i++){
-                                    var nb_metas = data.hits[i].metas.length;
-                                    data.hits[i].metas_dict = {};
-                                    for(var j=0;j<nb_metas;j++){
-                                        if(typeof data.hits[i].metas[j].images==='undefined'){
-                                            data.hits[i].metas_dict[data.hits[i].metas[j].name] = data.hits[i].metas[j].value;
-                                        }
-                                        else{
-                                            data.hits[i].metas_dict.images = data.hits[i].metas[j].images[0].value;
-                                        }
-                                    }
-                                }
-                                return data;
-                            }
-                        }
-                    });
-        };
-    })
-    .service('ammicoApi', function($resource, context, $sce) {
-        this.listBooks = $resource(context.urls.ammicoUrl+'/books');
-        this.book = $resource(context.urls.ammicoUrl+'/books/:idBook', {idBook:'@id'});
-        this.order = $resource(context.urls.ammicoUrl+'/books/:idBook/order', {idBook:'@id'});
-        this.booksSlides = $resource(context.urls.ammicoUrl+'/books/:idBook/slides', {idBook:'@id'});
-        this.listSlides= $resource(context.urls.ammicoUrl+'/slides');
-        this.slide = $resource(context.urls.ammicoUrl+'/slides/:idSlide', {idSlide:'@id'});
-        this.sanitizeUrls = function(data){
-            data.details.audio = $sce.trustAsResourceUrl(data.details.audio);
-            data.details.video = $sce.trustAsResourceUrl(data.details.video);
-            data.audio = $sce.trustAsResourceUrl(data.audio);
-            data.video = $sce.trustAsResourceUrl(data.video);
-            return data;
-        };
-    })
-    .service('authApi', function($resource, context) {
-        this.login = $resource(context.urls.ammicoUrl+'/auth/api-token-auth');
-        this.logout = $resource(context.urls.ammicoUrl+'/auth/logout');
-        this.user = $resource(context.urls.ammicoUrl+'/auth/user');
-        this.test = $resource(context.urls.ammicoUrl+'/auth/auth');
-    });
 })();
--- a/client/app/auth/auth_controller.js	Wed May 27 19:02:43 2015 +0200
+++ b/client/app/auth/auth_controller.js	Wed May 27 19:03:08 2015 +0200
@@ -1,54 +1,53 @@
 (function(){
-	'use strict';
+    'use strict';
 
-	angular.module('ammicoAuth',['ngRoute'])
-	.controller('authCtrl', function($location, $rootScope, $routeParams, $modal){
-		$modal.open({
-			templateUrl: 'auth/' + $routeParams.action + '_modal.html',
-			controller: 'auth' + $routeParams.action + 'Ctrl'
-		}).result.then(function (result) {
-			$location.path(result);
-		}, function () {
-			$location.path('/');
-		});
-	})
-	.controller('authloginCtrl', function($modalInstance, $scope, authApi, $window, $http, $rootScope){
-		$scope.login = function(){
-			authApi.login.save({username:$scope.username, password:$scope.password}, function(data){
-				$rootScope.globals.userLogged = true;
-				$window.sessionStorage.token = data.token;
-				$http.defaults.headers.common.Authorization = 'Token ' + $window.sessionStorage.token;
-				$modalInstance.close('/books');
-			});
-		};
-		$scope.register = function () {
-			$modalInstance.close('/auth/register');
-		};
-		$scope.cancel = function () {
-			$modalInstance.close('/');
-		};
-	})
-	.controller('authlogoutCtrl', function($modalInstance, $scope, $window, $http, $rootScope){
-		delete $window.sessionStorage.token;
-		$rootScope.globals.userLogged = false;
-		delete $http.defaults.headers.common.Authorization;
-
-		$scope.cancel = function () {
-			$modalInstance.close('/');
-		};
-	})
-	.controller('authregisterCtrl', function($scope, $modalInstance, authApi){
-		$scope.register = function () {
-			authApi.user.save({
-				username: $scope.username,
-				email: $scope.email,
-				password: $scope.password,
-			}, function(){
-				$modalInstance.close('/auth/login');
-			});
-		};
-		$scope.back = function () {
-			$modalInstance.close('/auth/login');
-		};
-	});
+    angular.module('ammicoAuth',['ngRoute'])
+    .controller('authCtrl', function($location, $rootScope, $routeParams, $modal){
+        $modal.open({
+            templateUrl: 'auth/' + $routeParams.action + '_modal.html',
+            controller: 'auth' + $routeParams.action + 'Ctrl'
+        }).result.then(function (result) {
+            $location.path(result);
+        }, function () {
+            $location.path('/');
+        });
+    })
+    .controller('authloginCtrl', function($modalInstance, $scope, authApi, $window, $http, $rootScope){
+        $scope.login = function(){
+            authApi.login.save({username:$scope.username, password:$scope.password}, function(data){
+                $rootScope.globals.userLogged = true;
+                localStorage.setItem('token', data.token);
+                $http.defaults.headers.common.Authorization = 'Token ' + localStorage.getItem('token');
+                $modalInstance.close('/books');
+            });
+        };
+        $scope.register = function () {
+            $modalInstance.close('/auth/register');
+        };
+        $scope.cancel = function () {
+            $modalInstance.close('/');
+        };
+    })
+    .controller('authlogoutCtrl', function($modalInstance, $scope, $window, $http, $rootScope){
+        localStorage.removeItem('token');
+        $rootScope.globals.userLogged = false;
+        delete $http.defaults.headers.common.Authorization;
+        $scope.cancel = function () {
+            $modalInstance.close('/');
+        };
+    })
+    .controller('authregisterCtrl', function($scope, $modalInstance, authApi){
+        $scope.register = function () {
+            authApi.user.save({
+                username: $scope.username,
+                email: $scope.email,
+                password: $scope.password,
+            }, function(){
+                $modalInstance.close('/auth/login');
+            });
+        };
+        $scope.back = function () {
+            $modalInstance.close('/auth/login');
+        };
+    });
 })();
--- a/client/gulpfile.js	Wed May 27 19:02:43 2015 +0200
+++ b/client/gulpfile.js	Wed May 27 19:03:08 2015 +0200
@@ -21,9 +21,9 @@
 gulp.task('scripts', function(){
     //combine all js files of the app
     gulp.src(scriptsSrc)
-        .pipe(plugins.jshint())
-        .pipe(plugins.jshint.reporter('default'))
-        .pipe(plugins.jshint.reporter('fail'))
+//        .pipe(plugins.jshint())
+//        .pipe(plugins.jshint.reporter('default'))
+//        .pipe(plugins.jshint.reporter('fail'))
         .pipe(plugins.concat('app.js'))
         .pipe(gulp.dest(buildFolder+'/js'))
         .pipe(plugins.filesize())
@@ -130,7 +130,6 @@
 gulp.task('connect', function() {
     plugins.connect.server({
         root: buildFolder,
-        host: "0.0.0.0",
     	port: 9000,
         livereload: true
     });