Commit 64c06fcf authored by Evren Kutar's avatar Evren Kutar

add service file temporary to controller file

save method cmd do
parent dea59e92
......@@ -7,9 +7,9 @@ app.config(['$routeProvider', function ($routeProvider) {
loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('components/auth/auth_controller.js');
}],
loadMyService: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('components/auth/auth_service.js');
}],
//loadMyService: ['$ocLazyLoad', function ($ocLazyLoad) {
// return $ocLazyLoad.load('components/auth/auth_service.js');
//}],
loadMyService2: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('zetalib/forms/form_service.js');
}],
......
......@@ -44,4 +44,83 @@ auth.controller('LoginCtrl', function ($scope, $q, $timeout, $routeParams, Gener
console.log("not valid");
}
}
});
auth.factory('LoginService', function ($http, $rootScope, $location, $log, $cookies, $window, Session, RESTURL) {
var loginService = {};
loginService.login = function (url, credentials) {
credentials = {login_crd: credentials, cmd: "do"};
return $http
.post(RESTURL.url + url, credentials)
.success(function (data, status, headers, config) {
//$window.sessionStorage.token = data.token;
$rootScope.loggedInUser = true;
})
.error(function (data, status, headers, config) {
// Erase the token if the user fails to log in
//delete $window.sessionStorage.token;
// Handle login errors here
return data;
});
//.then(function (res) {
// $log.info(res.data[0]);
// res.data = res.data[0];
// if (res.data.success) {
// $rootScope.loggedInUser = true;
// $location.path("/dashboard");
// var session = Session.create(res.data.id, res.data.user.id,
// res.data.user.role);
// $log.info(session);
// $cookies.put('sessionId', 123456);
// console.log($cookies.getAll());
// return res.data.user;
// }
//});
};
loginService.logout = function() {
console.log("logout");
$http.post(RESTURL.url + 'logout', {}).then(function(){
$rootScope.loggedInUser = false;
$location.path("/login");
});
console.log("loggedout");
};
loginService.isAuthenticated = function () {
return !!Session.userId;
};
loginService.isAuthorized = function (authorizedRoles) {
if (!angular.isArray(authorizedRoles)) {
authorizedRoles = [authorizedRoles];
}
return (loginService.isAuthenticated() &&
loginService.indexOf(Session.userRole) !== -1);
};
loginService.isValidEmail = function (email) {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
return re.test(email);
};
return loginService;
});
// TODO: initial service not working!!
auth.service('Session', function () {
this.create = function (sessionId, userId, userRole) {
this.id = sessionId;
this.userId = userId;
this.userRole = userRole;
};
this.destroy = function () {
this.id = null;
this.userId = null;
this.userRole = null;
};
});
\ No newline at end of file
......@@ -46,10 +46,10 @@ form_generator.factory('Generator', function ($http, $q, $log, $timeout, RESTURL
generator.submit = function ($scope) {
if($scope.object_id) {
var get_diff = FormDiff.get_diff($scope.model, $scope.initialModel);
var data = {"object_id": $scope.id, "form": get_diff};
var data = {"object_id": $scope.id, "form": get_diff, "cmd": "do"};
}
else {
data = $scope.model;
data = {"form": $scope.model, "cmd": "do"};
}
$http.post(generator.makeUrl($scope.url), data).then(function (res) {
// todo: for now fake rest api returns 'ok' no data to
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment