Commit 571ba9fd authored by Evren Kutar's avatar Evren Kutar

form service kullanımı ve url değişikliği

interceptor'den sayfayı ayıklama
parent d1e16543
......@@ -39,11 +39,10 @@ var app = angular.module(
* Based on the environment it changes from dev to prod
*/
constant("RESTURL", (function () {
var dev = "http://127.0.0.1:3000/api/";
var dev = "http://127.0.0.1:8000/";
var prod = "";
var ENV = "dev"; // change to prod in production
return ENV == "dev" ? {url: dev} : {url: prod};
//return "http://127.0.0.1:3000/api/";
})()).
/**
* USER_ROLES and AUTH_EVENTS are constant for auth functions
......
......@@ -9,6 +9,12 @@ app.config(['$routeProvider', function ($routeProvider) {
}],
loadMyService: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('components/auth/auth_service.js');
}],
loadMyService2: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('zetalib/forms/form_service.js');
}],
loadMyService3: ['$ocLazyLoad', function ($ocLazyLoad) {
return $ocLazyLoad.load('zetalib/general.js');
}]
}
})
......
......@@ -12,73 +12,92 @@
// TODO: who field can be removed??
// todo: use zetalib/forms/form_service functions for email validation
var auth = angular.module('zaerp.auth', ['ngRoute', 'schemaForm', 'ngCookies']);
auth.controller('LoginCtrl', function ($scope, $q, $timeout, LoginService) {
$scope.schema =
{
title: "Login",
type: "object",
properties: {
email: {
type: "email",
title: "Email"
},
password: {
type: "string",
title: "Password"
},
remember: {
type: "boolean",
title: "Remember me?"
},
who: {
title: "Who are you?",
type: "string",
enum: ["student", "stuff", "dean"]
}
},
required: ["email", "password", "who"]
};
$scope.model = {
email: "user@example.com",
remember: false
};
$scope.form = [
{
key: "email",
type: "email",
validationMessages: {
'emailNotValid': 'Email is not valid!'
},
$asyncValidators: {
emailNotValid: function (value) {
var deferred = $q.defer();
$timeout(function () {
if (LoginService.isValidEmail(value)) {
deferred.resolve();
} else {
deferred.reject();
}
}, 500);
return deferred.promise;
}
var auth = angular.module('zaerp.auth', ['ngRoute', 'schemaForm', 'ngCookies', 'general']);
auth.controller('LoginCtrl', function ($scope, $q, $timeout, $routeParams, Generator, LoginService) {
$scope.url = 'simple_login';
// todo: change simple login when api ready
Generator.get_form($scope.url, $routeParams).then(function(d){
$scope.schema = d.schema;
$scope.form = d.form;
// model is the init data of the form or in edit templates
$scope.model = d.model ? d.model : {};
$scope.initialModel = angular.copy(d.model);
// for email validation add asyncvalidator
//$scope.form[0].$asyncValidators = Generator.asyncValidators;
// add submit button to the form todo: move this to form service
$scope.form.push(
{
type: "submit",
title: "Save"
}
},
{
key: "password",
type: "password"
},
"remember",
"who",
{
type: "submit",
title: "Save"
}
];
);
});
//$scope.schema =
//{
// title: "Login",
// type: "object",
// properties: {
// email: {
// type: "email",
// title: "Email"
// },
// password: {
// type: "string",
// title: "Password"
// },
// remember: {
// type: "boolean",
// title: "Remember me?"
// },
// who: {
// title: "Who are you?",
// type: "string",
// enum: ["student", "stuff", "dean"]
// }
// },
// required: ["email", "password", "who"]
//};
//$scope.model = {
// email: "user@example.com",
// remember: false
//};
//$scope.form = [
// {
// key: "email",
// type: "email",
// validationMessages: {
// 'emailNotValid': 'Email is not valid!'
// },
// $asyncValidators: {
// emailNotValid: function (value) {
// var deferred = $q.defer();
// $timeout(function () {
// if (LoginService.isValidEmail(value)) {
// deferred.resolve();
// } else {
// deferred.reject();
// }
// }, 500);
// return deferred.promise;
// }
// }
// },
// {
// key: "password",
// type: "password"
// },
// "remember",
// "who",
// {
// type: "submit",
// title: "Save"
// }
//];
$scope.onSubmit = function (form) {
$scope.$broadcast('schemaFormValidate');
if (form.$valid) {
LoginService.login($scope.model);
LoginService.login($scope.url, $scope.model);
}
else {
console.log("not valid");
......
......@@ -12,14 +12,14 @@
auth.factory('LoginService', function ($http, $rootScope, $location, $log, $cookies, Session, RESTURL) {
var loginService = {};
loginService.login = function (credentials) {
loginService.login = function (url, credentials) {
// TODO: change this getParams var to service to use app-wide
var getParams = "?";
for (var k in credentials) {
getParams += k + "=" + credentials[k] + "&";
}
return $http
.get(RESTURL.url + 'login' + getParams)
.get(RESTURL.url + url + getParams)
.then(function (res) {
$log.info(res.data[0]);
res.data = res.data[0];
......
......@@ -60,5 +60,20 @@ describe('form service module', function () {
})
);
it('should post form',
inject(function (Generator, $httpBackend, RESTURL) {
$httpBackend.expectGET(RESTURL.url + 'student/add')
.respond(200, [{data: 'OK'}]);
var cred = {email: 'test@test.com'};
Generator.submit('student/add', cred)
.then(function (data) {
expect(data).toEqual({data: 'OK'});
});
$httpBackend.flush();
})
);
});
});
\ No newline at end of file
......@@ -22,6 +22,9 @@ app.config(['$httpProvider', function ($httpProvider) {
},
'response': function (response) {
//Will only be called for HTTP up to 300
if(response.screen) {
location.path(response.screen);
}
return response;
},
'responseError': function (rejection) {
......
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