From 2c6a59d1956ac48b34682bfd46cc39af58454c47 Mon Sep 17 00:00:00 2001 From: Snir Date: Tue, 27 May 2014 11:12:42 +0300 Subject: [PATCH 1/4] Added user, security service & login page --- app/index.html | 5 +++ app/mock_data/login.json | 3 ++ app/scripts/app.js | 11 +++++- app/scripts/controllers/dashboard.js | 6 +++ app/scripts/controllers/login.js | 14 +++++++ app/scripts/controllers/main.js | 2 +- .../services/ethosiahttpinterceptor.js | 36 ----------------- app/scripts/services/user/security.js | 27 +++++++++++++ app/scripts/services/user/user.js | 15 +++++++ app/views/dashboard.html | 37 ++++++++++++++++++ app/views/login.html | 28 +++++++++++++ app/views/main.html | 39 ++++++++++++++++++- bower.json | 3 +- 13 files changed, 186 insertions(+), 40 deletions(-) create mode 100644 app/mock_data/login.json create mode 100644 app/scripts/controllers/dashboard.js create mode 100644 app/scripts/controllers/login.js delete mode 100644 app/scripts/services/ethosiahttpinterceptor.js create mode 100644 app/scripts/services/user/security.js create mode 100644 app/scripts/services/user/user.js create mode 100644 app/views/dashboard.html create mode 100644 app/views/login.html diff --git a/app/index.html b/app/index.html index 46fbe73..9fbbbb4 100644 --- a/app/index.html +++ b/app/index.html @@ -61,6 +61,7 @@ + @@ -69,7 +70,11 @@ + + + + diff --git a/app/mock_data/login.json b/app/mock_data/login.json new file mode 100644 index 0000000..816ddfc --- /dev/null +++ b/app/mock_data/login.json @@ -0,0 +1,3 @@ +{ + "token" : "21232f297a57a5a743894a0e4a801fc3" +} diff --git a/app/scripts/app.js b/app/scripts/app.js index 4940e77..f37f95e 100644 --- a/app/scripts/app.js +++ b/app/scripts/app.js @@ -6,7 +6,8 @@ angular 'ngResource', 'ngSanitize', 'ngRoute', - 'apiMock' + 'apiMock', + 'ngStorage' ]) .config(function ($routeProvider, apiMockProvider) { $routeProvider @@ -14,6 +15,14 @@ angular templateUrl: 'views/main.html', controller: 'MainCtrl' }) + .when('/login', { + templateUrl: 'views/login.html', + controller: 'LoginCtrl' + }) + .when('/dashboard', { + templateUrl: 'views/dashboard.html', + controller: 'DashboardCtrl' + }) .otherwise({ redirectTo: '/' }); diff --git a/app/scripts/controllers/dashboard.js b/app/scripts/controllers/dashboard.js new file mode 100644 index 0000000..6a3513c --- /dev/null +++ b/app/scripts/controllers/dashboard.js @@ -0,0 +1,6 @@ +'use strict'; + +angular.module('myApp') + .controller('DashboardCtrl', function ($scope) { + + }); diff --git a/app/scripts/controllers/login.js b/app/scripts/controllers/login.js new file mode 100644 index 0000000..4ab705e --- /dev/null +++ b/app/scripts/controllers/login.js @@ -0,0 +1,14 @@ +'use strict'; + +angular.module('myApp') + .controller('LoginCtrl', function ($scope, Security) { + $scope.user = {}; + + + $scope.login = function() { + Security.login(user, function(res) { + $location.path('/dashboard'); + }); + } + + }); diff --git a/app/scripts/controllers/main.js b/app/scripts/controllers/main.js index 3bb10d8..5a86150 100644 --- a/app/scripts/controllers/main.js +++ b/app/scripts/controllers/main.js @@ -1,5 +1,5 @@ 'use strict'; angular.module('myApp') - .controller('MainCtrl', function ($scope, Boxes) { + .controller('MainCtrl', function ($scope) { }); diff --git a/app/scripts/services/ethosiahttpinterceptor.js b/app/scripts/services/ethosiahttpinterceptor.js deleted file mode 100644 index c17ca00..0000000 --- a/app/scripts/services/ethosiahttpinterceptor.js +++ /dev/null @@ -1,36 +0,0 @@ -'use strict'; - -angular.module('myApp') - .factory('restfulHttpInterceptor', function ($q, BACKEND_URL) { - // Public API here - return { - /** - * This interceptor construct the path to get the data translated, - * check the config and according this generated the new path in relation - * the language selected by the user. - * - * @param config - * In the $http config add two new variables: - * - * - serverPredefined: true|false if the value is true take the constant - * WEB_URL defined in the config.js to set the server URI. Otherwise - * keep the original config.url. * - * - apiMock: true|false indicate if use the interceptor angular-apimock. - * to maintain compatibility. - * - * @returns {*} - */ - 'request': function(config) { - // Validate if use the server url defined in the constant. - if (config.serverPredefined) { - config._url = BACKEND_URL; - - // Have the condition to work with the module angular-apimock - // https://github.com/seriema/angular-apimock - config.url = (angular.isDefined(config.apiMock) && config.apiMock) ? config.url : config._url + config.url; - } - - return config || $q.when(config); - } - }; - }); diff --git a/app/scripts/services/user/security.js b/app/scripts/services/user/security.js new file mode 100644 index 0000000..f82e222 --- /dev/null +++ b/app/scripts/services/user/security.js @@ -0,0 +1,27 @@ +'use strict'; + +angular.module('myApp'). + service('Security', function($http, User) { + + this.status = function(success, error) { + $http.get('/status').success(function(res) { + User.setDetails(res); + success(); + }).error(error); + } + + this.login = function(user, success, error) { + $http.post('/login').success(function(res) { + User.setUserDetails(res); + success(); + }).error(error); + } + + this.logout = function(success, error) { + $http.post('/logout').success(function(res) { + User.clear(); + success(); + }).error(error); + } + + }); diff --git a/app/scripts/services/user/user.js b/app/scripts/services/user/user.js new file mode 100644 index 0000000..e74bfff --- /dev/null +++ b/app/scripts/services/user/user.js @@ -0,0 +1,15 @@ +'use strict'; + +angular.module('myApp'). + service('User', function($localStorage) { + this.$storage = $localStorage; + + this.setUserDetails = function(userDetails) { + this.$storage.userDetails = userDetails; + } + + this.clear = function() { + delete this.$storage.userDetails; + } + + }) diff --git a/app/views/dashboard.html b/app/views/dashboard.html new file mode 100644 index 0000000..5a6e03b --- /dev/null +++ b/app/views/dashboard.html @@ -0,0 +1,37 @@ +
+ +

Restful client example

+
+ +
+

Welcome to dashboard.

+

+ I'm Yeoman
+ Always a pleasure scaffolding your apps. +

+

Splendid!

+
+ +
+

HTML5 Boilerplate

+

+ HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites. +

+ +

Angular

+

+ AngularJS is a toolset for building the framework most suited to your application development. +

+ +

Karma

+

Spectacular Test Runner for JavaScript.

+
+ + diff --git a/app/views/login.html b/app/views/login.html new file mode 100644 index 0000000..c44ee0a --- /dev/null +++ b/app/views/login.html @@ -0,0 +1,28 @@ +
+ +

Restful client example

+
+ + +
+

Login

+

+ + +

+

+ + +

+

Login

+
+ + + diff --git a/app/views/main.html b/app/views/main.html index 60e65c3..2c8c203 100644 --- a/app/views/main.html +++ b/app/views/main.html @@ -1 +1,38 @@ -Main view +
+ +

Restful client example

+
+ + +
+

'Allo, 'Allo!

+

+ I'm Yeoman
+ Always a pleasure scaffolding your apps. +

+

Splendid!

+
+ +
+

HTML5 Boilerplate

+

+ HTML5 Boilerplate is a professional front-end template for building fast, robust, and adaptable web apps or sites. +

+ +

Angular

+

+ AngularJS is a toolset for building the framework most suited to your application development. +

+ +

Karma

+

Spectacular Test Runner for JavaScript.

+
+ + diff --git a/bower.json b/bower.json index 694e82c..d63882c 100644 --- a/bower.json +++ b/bower.json @@ -11,7 +11,8 @@ "angular-cookies": "1.2.16", "angular-sanitize": "1.2.16", "angular-route": "1.2.16", - "angular-apimock": "*" + "angular-apimock": "*", + "ngstorage": "~0.3.0" }, "devDependencies": { "angular-mocks": "1.2.16", From 19b055f3c141b0d37755a23cfddc3b52e511c548 Mon Sep 17 00:00:00 2001 From: Snir Date: Mon, 9 Jun 2014 10:02:56 +0300 Subject: [PATCH 2/4] Security layer service --- app/scripts/controllers/login.js | 13 ++++++++++--- app/scripts/services/user/security.js | 28 ++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/scripts/controllers/login.js b/app/scripts/controllers/login.js index 4ab705e..f31a505 100644 --- a/app/scripts/controllers/login.js +++ b/app/scripts/controllers/login.js @@ -1,14 +1,21 @@ 'use strict'; angular.module('myApp') - .controller('LoginCtrl', function ($scope, Security) { + .controller('LoginCtrl', ['$scope', '$cookies', 'Security', function($scope, $cookies, Security) { $scope.user = {}; $scope.login = function() { - Security.login(user, function(res) { + Security.login($scope.user, function(res) { + // Success. + $location.path('/dashboard'); + }, + function(res) { + // Error. + + console.log('Error in login'); }); } - }); + }]); diff --git a/app/scripts/services/user/security.js b/app/scripts/services/user/security.js index f82e222..fd79165 100644 --- a/app/scripts/services/user/security.js +++ b/app/scripts/services/user/security.js @@ -4,21 +4,35 @@ angular.module('myApp'). service('Security', function($http, User) { this.status = function(success, error) { - $http.get('/status').success(function(res) { - User.setDetails(res); - success(); + $http.get('http://127.0.0.1/login.php?status=1').success(function(res) { + if (res.status !== "undefined" && res.token) { + User.setDetails(res); + success(); + } }).error(error); } this.login = function(user, success, error) { - $http.post('/login').success(function(res) { - User.setUserDetails(res); - success(); + $http.post('http://127.0.0.1/login.php', user, + { + headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, + transformRequest: function(data){ + return $.param(data); + } + } + ).success(function(res) { + if (res.status !== "undefined" && res.token) { + User.setUserDetails(res); + success(); + } + else { + error(); + } }).error(error); } this.logout = function(success, error) { - $http.post('/logout').success(function(res) { + $http.post('http://127.0.0.1/login.php?logout=1').success(function(res) { User.clear(); success(); }).error(error); From b56c7c3a61fc7130d0435c84a9f9c2165bf8af07 Mon Sep 17 00:00:00 2001 From: Snir Segal Date: Mon, 9 Jun 2014 00:33:41 -0700 Subject: [PATCH 3/4] Replace service to return promise --- app/scripts/controllers/login.js | 18 +++++++++++++----- app/scripts/services/user/security.js | 25 +++++++++++++++++-------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/scripts/controllers/login.js b/app/scripts/controllers/login.js index f31a505..e854c18 100644 --- a/app/scripts/controllers/login.js +++ b/app/scripts/controllers/login.js @@ -1,21 +1,29 @@ 'use strict'; angular.module('myApp') - .controller('LoginCtrl', ['$scope', '$cookies', 'Security', function($scope, $cookies, Security) { + .controller('LoginCtrl', ['$scope', '$cookies', 'Security', 'User', function($scope, $cookies, Security, User) { $scope.user = {}; $scope.login = function() { - Security.login($scope.user, function(res) { + Security.login($scope.user).then(function(data) { + console.log(data); + if (data.status !== "undefined" && data.token) { + User.setUserDetails(data); + } + }, function(data, status, headers, config) { + console.log("Error: "); + console.table(data, status, headers, config); + }); + //, function(res) { // Success. - $location.path('/dashboard'); - }, + /* }, function(res) { // Error. console.log('Error in login'); - }); + });*/ } }]); diff --git a/app/scripts/services/user/security.js b/app/scripts/services/user/security.js index fd79165..f6145c0 100644 --- a/app/scripts/services/user/security.js +++ b/app/scripts/services/user/security.js @@ -1,10 +1,10 @@ 'use strict'; angular.module('myApp'). - service('Security', function($http, User) { + service('Security', ['$http', '$q', 'User', function($http, $q, User) { this.status = function(success, error) { - $http.get('http://127.0.0.1/login.php?status=1').success(function(res) { + $http.get('http://127.0.0.1/restful-login.php?status=1').success(function(res) { if (res.status !== "undefined" && res.token) { User.setDetails(res); success(); @@ -12,15 +12,24 @@ angular.module('myApp'). }).error(error); } - this.login = function(user, success, error) { - $http.post('http://127.0.0.1/login.php', user, + this.login = function(user) { + + var deferred = $q.defer(); + $http.post('http://127.0.0.1/restful-login.php', user, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, transformRequest: function(data){ return $.param(data); } } - ).success(function(res) { + ).success(function(data) { + deferred.resolve(data); + }).error(function(data, status) { + deferred.reject(status); + }); + + return deferred.promise; + /*.success(function(res) { if (res.status !== "undefined" && res.token) { User.setUserDetails(res); success(); @@ -28,14 +37,14 @@ angular.module('myApp'). else { error(); } - }).error(error); + }).error(error);*/ } this.logout = function(success, error) { - $http.post('http://127.0.0.1/login.php?logout=1').success(function(res) { + $http.post('http://127.0.0.1/restful-login.php?logout=1').success(function(res) { User.clear(); success(); }).error(error); } - }); + }]); From e1a67be8eda09e7e8c3baff552074e49f36d19a4 Mon Sep 17 00:00:00 2001 From: Snir Date: Tue, 10 Jun 2014 10:03:16 +0300 Subject: [PATCH 4/4] Service structure fixes --- app/index.html | 2 +- app/scripts/controllers/login.js | 50 +++++++++++-------- .../services/user/{security.js => auth.js} | 23 ++------- app/scripts/services/user/user.js | 10 ++-- app/views/dashboard.html | 1 + app/views/login.html | 1 + 6 files changed, 43 insertions(+), 44 deletions(-) rename app/scripts/services/user/{security.js => auth.js} (57%) diff --git a/app/index.html b/app/index.html index 9fbbbb4..e67a40b 100644 --- a/app/index.html +++ b/app/index.html @@ -71,7 +71,7 @@ - + diff --git a/app/scripts/controllers/login.js b/app/scripts/controllers/login.js index e854c18..037707f 100644 --- a/app/scripts/controllers/login.js +++ b/app/scripts/controllers/login.js @@ -1,29 +1,39 @@ 'use strict'; angular.module('myApp') - .controller('LoginCtrl', ['$scope', '$cookies', 'Security', 'User', function($scope, $cookies, Security, User) { - $scope.user = {}; + .controller('LoginCtrl', ['$scope', '$cookies', '$location', 'Auth', 'User', function($scope, $cookies, $location, Auth, User) { + + Auth.status(function() { + if (User.getUserDetails().status == true) { + $location.url('/dashboard'); + } + else { + + } + }, + function() { + User.clear(); + }); + $scope.user = {}; + $scope.login_error = null; $scope.login = function() { - Security.login($scope.user).then(function(data) { - console.log(data); - if (data.status !== "undefined" && data.token) { - User.setUserDetails(data); - } - }, function(data, status, headers, config) { - console.log("Error: "); - console.table(data, status, headers, config); - }); - //, function(res) { - // Success. - - /* }, - function(res) { - // Error. - - console.log('Error in login'); - });*/ + + Auth.login($scope.user) + .success(function(data) { + if (!data.error) { + User.setUserDetails(data); + + $location.url('/dashboard'); + } + else { + $scope.login_error = data.description; + } + }) + .error(function(data, status, headers, config) { + $scope.login_error = 'Login failed.'; + }); } }]); diff --git a/app/scripts/services/user/security.js b/app/scripts/services/user/auth.js similarity index 57% rename from app/scripts/services/user/security.js rename to app/scripts/services/user/auth.js index f6145c0..731df40 100644 --- a/app/scripts/services/user/security.js +++ b/app/scripts/services/user/auth.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('myApp'). - service('Security', ['$http', '$q', 'User', function($http, $q, User) { + service('Auth', ['$http', '$q', 'User', function($http, $q, User) { this.status = function(success, error) { $http.get('http://127.0.0.1/restful-login.php?status=1').success(function(res) { @@ -13,31 +13,14 @@ angular.module('myApp'). } this.login = function(user) { - - var deferred = $q.defer(); - $http.post('http://127.0.0.1/restful-login.php', user, + return $http.post('http://127.0.0.1/restful-login.php', user, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, transformRequest: function(data){ return $.param(data); } } - ).success(function(data) { - deferred.resolve(data); - }).error(function(data, status) { - deferred.reject(status); - }); - - return deferred.promise; - /*.success(function(res) { - if (res.status !== "undefined" && res.token) { - User.setUserDetails(res); - success(); - } - else { - error(); - } - }).error(error);*/ + ); } this.logout = function(success, error) { diff --git a/app/scripts/services/user/user.js b/app/scripts/services/user/user.js index e74bfff..9f9aacd 100644 --- a/app/scripts/services/user/user.js +++ b/app/scripts/services/user/user.js @@ -2,14 +2,18 @@ angular.module('myApp'). service('User', function($localStorage) { - this.$storage = $localStorage; + this.storage = $localStorage; this.setUserDetails = function(userDetails) { - this.$storage.userDetails = userDetails; + this.storage.userDetails = userDetails; + } + + this.getUserDetails = function() { + return this.storage.userDetails; } this.clear = function() { - delete this.$storage.userDetails; + delete this.storage.userDetails; } }) diff --git a/app/views/dashboard.html b/app/views/dashboard.html index 5a6e03b..062def9 100644 --- a/app/views/dashboard.html +++ b/app/views/dashboard.html @@ -3,6 +3,7 @@
  • Home
  • Dashboard
  • Login
  • +
  • Login
  • Registration
  • Restful client example

    diff --git a/app/views/login.html b/app/views/login.html index c44ee0a..1ad1334 100644 --- a/app/views/login.html +++ b/app/views/login.html @@ -11,6 +11,7 @@

    Restful client example

    Login

    +
    {{ login_error }}