Use angular-cookirs

This commit is contained in:
manzerbredes 2016-02-17 14:01:13 +01:00
parent 8c6b1cecd3
commit 7e5db6b542
19 changed files with 119 additions and 27 deletions

View file

@ -3,7 +3,7 @@
* The main app module instance
* @type angular.module.angular-1_3_6_L1749.moduleInstance
*/
var mainApp=angular.module("mainApp",['ngRoute', 'ngSanitize']);
var mainApp=angular.module("mainApp",['ngRoute', 'ngSanitize', 'ngCookies']);
/**
* Configure routeProvider
@ -29,6 +29,5 @@ mainApp.config(['$routeProvider', function($routeProvider){
*/
mainApp.config(['$httpProvider', function($httpProvider){
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
}]);

View file

@ -0,0 +1 @@
loic@Manzerbredes.home.30343:1455008378

View file

@ -9,14 +9,20 @@
*/
mainApp.controller('loginCtrl', ['$scope','$sce','Identity', function ($scope,$sce, Identity)
{
// Define default states
$('#loginModal').modal({backdrop: 'static', keyboard: false});
// Check for login and define default states
if(!Identity.isAlreadyLogin()){
$('#loginModal').modal({backdrop: 'static', keyboard: false});
}
$scope.$on('logoutEvent', function(){
$('#loginModal').modal({backdrop: 'static', keyboard: false});
});
$('#loadingLoginButton').hide();
$('#failedToLoginAlert').hide();
$('#loginButton').click(function(){
$scope.loginAction=function(){
// Begin login state for template
$('#loginButton').hide();
$('#loadingLoginButton').show();
@ -49,6 +55,6 @@ mainApp.controller('loginCtrl', ['$scope','$sce','Identity', function ($scope,$s
// Try to login
Identity.login(username, password, projectname, responseCallback);
});
};
}]);

View file

@ -6,9 +6,14 @@
* @param {$scope} $scope The $scope service from angular
* @param {Identity} The Identity service
*/
mainApp.controller('statusCtrl', ['$scope','Identity', function ($scope, Identity)
mainApp.controller('statusCtrl', ['$scope','Identity', '$rootScope', function ($scope, Identity, $rootScope)
{
$scope.profile=Identity.profile;
$scope.logout=function(){
Identity.logout();
$rootScope.$broadcast('logoutEvent');
};
}]);

View file

@ -1,5 +1,5 @@
mainApp.factory('Identity',[ '$http', function($http){
mainApp.factory('Identity',[ '$http', '$cookies', function($http, $cookies){
/* Create profile structure to store informations
* about current session
@ -9,7 +9,72 @@ mainApp.factory('Identity',[ '$http', function($http){
profile.projectname=null;
profile.token=null;
/* var tokenFragment=9;
var saveTokenInCookies=function(){
var i=0;
var currentStart=0;
var currentEnd=parseInt(profile.token.length/tokenFragment);
var facto=currentEnd;
alert("current ren" + currentEnd);
for(i=0;i<tokenFragment-1;i++){
$cookies.put("token-"+i, profile.token.substring(currentStart, currentEnd));
currentStart=currentEnd+1;
currentEnd=currentEnd+facto;
}
$cookies.put("token-"+(i+1), profile.token.substring(currentEnd, profile.token.length));
};
var loadTokenInCookies=function(){
var i=0;
profile.token=$cookies.get("token-0");
for(i=1;i<tokenFragment;i++){
profile.token=profile.token+$cookies.get("token-"+i)
}
};*/
var saveCookieForSession=function(){
$cookies.putObject('profile', 5);
//saveTokenInCookies();
};
var isAlreadyLogin=function(){
var profileInCookie=$cookies.getObject('profile');
console.log(profileInCookie);
if(typeof profileInCookie !== 'undefined'){
angular.extend(profile, profileInCookie);
return true;
}
return false;
}
var logout=function(){
$cookies.remove('profile');
}
/**
*
* @param {string} response The response to parse
@ -17,6 +82,7 @@ mainApp.factory('Identity',[ '$http', function($http){
* @returns {requestParserResult} Formated data
*/
var parseLoginAnswer=function(response, failedToSendRequest){
var requestParserResult={};
requestParserResult.status=1;
@ -24,7 +90,8 @@ mainApp.factory('Identity',[ '$http', function($http){
if (typeof response.data.token !== 'undefined') {
requestParserResult.status=0;
profile.token=response.data.token;
profile.token=response.data.token;
saveCookieForSession();
}
else if(failedToSendRequest){
requestParserResult.failReason="Failed to send request";
@ -48,7 +115,7 @@ mainApp.factory('Identity',[ '$http', function($http){
* @param {function} function to call when data is avalaible
*/
var login=function(username, password,projectname, callback){
// Set profile information (early)
profile.username=username;
profile.projectname=projectname;
@ -67,7 +134,9 @@ mainApp.factory('Identity',[ '$http', function($http){
// Return services objects
return {
login: login,
profile: profile
profile: profile,
isAlreadyLogin: isAlreadyLogin,
logout:logout
};

View file

@ -1,24 +1,27 @@
mainApp.factory('Image',[ '$http', 'Identity', function($http, Identity){
var httpResponse;
var uploadImage=function(image){
var parseUploadImageAnswer=function(response, failedToSendRequest){
};
var parseUploadImageRequest=function(){
var getImages=function(callback){
var result=$http.post('../server/index.php',
$.param({"token" : Identity.profile.token, "task" : "Image"}));
};
var getResponse=function(){
return parseUploadImageRequest(httpResponse);
};
// Return services objects
return {
uploadImage: uploadImage,
getResponse: getResponse
uploadImage: uploadImage
};