First commit

This commit is contained in:
2015-09-10 14:02:43 +00:00
commit ab624930df
25 changed files with 933 additions and 0 deletions

33
public/js/app.js Normal file
View File

@@ -0,0 +1,33 @@
(function() {
'use strict';
var HOST = 'http://cloudbudget.pavnay.fr/api';
angular
.module('cloudbudget', ['ngRoute', 'routes', 'ngCookies'])
.constant('apiRoutes', {
'host': HOST,
'port': "80",
'login': HOST + '/users/login',
'register': HOST + '/users',
'unregister': HOST + '/users/'
})
.run(run);
run.$inject = ['$rootScope', '$location', '$cookieStore', '$http', '$filter'];
function run( $rootScope, $location, $cookieStore, $http, $filter) {
$rootScope.globals = $cookieStore.get('globals') || {};
if( $rootScope.globals.user && $rootScope.globals.user.token) {
$http.defaults.headers.common['Authorization'] = 'JWT ' + $rootScope.globals.user.token;
}
$rootScope.$on('$locationChangeStart', function(event, next, current) {
var restrictedPage = ['/login', '/register'].indexOf($location.path()) === -1;
var loggedIn = $rootScope.globals.user;
if( restrictedPage && !loggedIn ) {
$location.path('/login');
}
});
}
})();

34
public/js/routes.js Normal file
View File

@@ -0,0 +1,34 @@
(function() {
'use strict';
angular
.module('routes', [])
.config(config);
config.$inject = ['$routeProvider', '$locationProvider'];
function config($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
controller: 'HomeController',
templateUrl: 'home/home.view.html',
controllerAs: 'vm'
})
.when('/login', {
controller: 'LoginController',
templateUrl: 'login/login.view.html',
controllerAs: 'vm'
})
.when('/register', {
controller: 'RegisterController',
templateUrl: 'register/register.view.html',
controllerAs: 'vm'
})
.otherwise({redirectTo: '/login'});
$locationProvider.html5Mode(true);
};
})();

View File

@@ -0,0 +1,57 @@
(function() {
'use strict';
angular
.module('cloudbudget')
.factory('AuthenticationService', AuthenticationService);
AuthenticationService.$inject = ['$http', '$cookieStore', '$rootScope', 'apiRoutes'];
function AuthenticationService($http, $cookieStore, $rootScope, apiRoutes) {
var service = {};
service.login = login;
service.setCredentials = setCredentials;
service.clearCredentials = clearCredentials;
return service;
function login(username, password ) {
return $http.post( apiRoutes.login, {username: username, password: password})
.then(function(response, status) {
if( status === 200 ) {
return {
success: true,
user: response
};
} else {
return {
success: false,
message: response.message
};
}
}, function(response) {
return {
success: false,
message: 'An error occurs'
};
});
}
function setCredentials(user) {
$rootScope.globals = {
user: user,
token: user.token
}
$http.defaults.headers.common['Authorization'] = 'JWT ' + user.token;
$cookieStore.put('globals', $rootScope.globals);
console.log( $cookieStore.get('globals'));
}
function clearCredentials() {
$rootScope.globals = {};
$http.defaults.headers.common.Authorization = 'JWT ';
$cookieStore.remove('globals');
}
}
})();

View File

@@ -0,0 +1,53 @@
(function() {
'use strict';
angular
.module('cloudbudget')
.factory('FlashService', FlashService);
FlashService.$inject = ['$rootScope'];
function FlashService($rootScope) {
var service = {};
service.success = success;
service.error = error;
initService();
return service;
function initService() {
$rootScope.$on('$locationChangeStart', function() {
clearFlashMessage();
});
function clearFlashMessage() {
var flash = $rootScope.flash;
if( flash ) {
if( !flash.keepAfterLocationChange ) {
delete $rootScope.flash;
} else {
flash.keepAfterLocationChange = false;
}
}
}
}
function success(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'success',
keepAfterLocationChange: keepAfterLocationChange
};
}
function error(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'error',
keepAfterLocationChange: keepAfterLocationChange
};
}
}
})();

View File

@@ -0,0 +1,38 @@
(function() {
'use strict';
angular
.module('cloudbudget')
.factory('UserService', UserService);
UserService.$inject = ['$http', 'apiRoutes'];
function UserService($http, apiRoutes) {
var service = {};
service.register = register;
service.unregister = unregister;
return service;
function register(user) {
return $http.post( apiRoutes.register, user)
.then(handleSuccess, handleError('Error creating user'));
}
function unregister(id) {
return $http.delete( apiRoutes.unregister + id)
.then(handleSuccess, handleError('Error deleting user'));
}
function handleSuccess(response) {
return {success: true, user: response.data};
}
function handleError(error) {
return function() {
return {success: false, message: error};
};
}
}
})();