From 049492ca1a7a3cdc486a18e714102554cdeccdba Mon Sep 17 00:00:00 2001 From: nau Date: Sun, 28 Aug 2016 19:37:46 +0200 Subject: [PATCH] signup and login system implemented in the app --- app/www/js/controllers.js | 84 +++++++++++++++++++++++++++++----- app/www/templates/login.html | 2 +- app/www/templates/menu.html | 8 +++- app/www/templates/signup.html | 42 +++++++++++++++++ app/www/templates/travel.html | 18 ++++---- app/www/templates/travels.html | 2 +- 6 files changed, 134 insertions(+), 22 deletions(-) create mode 100644 app/www/templates/signup.html diff --git a/app/www/js/controllers.js b/app/www/js/controllers.js index a10e19b..f1bc3c8 100644 --- a/app/www/js/controllers.js +++ b/app/www/js/controllers.js @@ -1,13 +1,13 @@ //var urlapi="http://localhost:3000/api/"; var urlapi="http://192.168.1.40:3000/api/"; -localStorage.setItem("c_username", "user2"); -localStorage.setItem("c_token", ""); +//localStorage.setItem("c_username", "user2"); +//localStorage.setItem("c_token", ""); angular.module('starter.controllers', []) -.controller('AppCtrl', function($scope, $ionicModal, $timeout) { +.controller('AppCtrl', function($scope, $ionicModal, $timeout, $http, $window) { // With the new view caching in Ionic, Controllers are only called // when they are recreated or on app start, instead of every page change. // To listen for when this page is active (for example, to refresh data), @@ -17,33 +17,95 @@ angular.module('starter.controllers', []) // Form data for the login modal $scope.loginData = {}; + $scope.signupData= {}; + $scope.storageusername=localStorage.getItem("c_username"); // Create the login modal that we will use later $ionicModal.fromTemplateUrl('templates/login.html', { scope: $scope }).then(function(modal) { - $scope.modal = modal; + $scope.modalLogin = modal; + }); + $ionicModal.fromTemplateUrl('templates/signup.html', { + scope: $scope + }).then(function(modal) { + $scope.modalSignup = modal; }); // Triggered in the login modal to close it $scope.closeLogin = function() { - $scope.modal.hide(); + $scope.modalLogin.hide(); + }; + $scope.closeSignup = function() { + $scope.modalSignup.hide(); }; // Open the login modal $scope.login = function() { - $scope.modal.show(); + $scope.modalLogin.show(); + }; + $scope.signup = function() { + $scope.modalSignup.show(); }; // Perform the login action when the user submits the login form $scope.doLogin = function() { console.log('Doing login', $scope.loginData); - // Simulate a login delay. Remove this and replace with your login - // code if using a login system - $timeout(function() { - $scope.closeLogin(); - }, 1000); + $http({ + url: urlapi + 'auth', + method: "POST", + data: $scope.loginData + }) + .then(function(response) { + // success + console.log("response: "); + console.log(response.data); + if(response.data.success==true) + { + console.log("login successful"); + localStorage.setItem("c_username", $scope.loginData.username); + localStorage.setItem("c_token", response.data.token); + }else{ + console.log("login failed"); + } + $timeout(function() { + $scope.closeLogin(); + $window.location.reload(true); + }, 1000); + + }, + function(response) { // optional + // failed + }); + + }; + $scope.doSignup = function() { + console.log('Doing signup', $scope.signupData); + + $http({ + url: urlapi + 'users', + method: "POST", + data: $scope.signupData + }) + .then(function(response) { + // success + console.log("response: "); + console.log(response.data); + $timeout(function() { + $scope.closeSignup(); + }, 1000); + + }, + function(response) { // optional + // failed + }); + + }; + $scope.logout = function(){ + localStorage.removeItem("c_username"); + localStorage.removeItem("c_token"); + $window.location.reload(true); }; }) diff --git a/app/www/templates/login.html b/app/www/templates/login.html index 793bd7f..1123cc7 100644 --- a/app/www/templates/login.html +++ b/app/www/templates/login.html @@ -2,7 +2,7 @@

Login

- +
diff --git a/app/www/templates/menu.html b/app/www/templates/menu.html index 838f5e6..3d37231 100644 --- a/app/www/templates/menu.html +++ b/app/www/templates/menu.html @@ -18,7 +18,10 @@ - + + Signup + + Login @@ -33,6 +36,9 @@ Settings + + Logout + diff --git a/app/www/templates/signup.html b/app/www/templates/signup.html new file mode 100644 index 0000000..e972d45 --- /dev/null +++ b/app/www/templates/signup.html @@ -0,0 +1,42 @@ + + +

Signup

+
+ +
+
+ +
+
+ + + + + + + + +
+
+
+
diff --git a/app/www/templates/travel.html b/app/www/templates/travel.html index fd366ed..6c216f7 100644 --- a/app/www/templates/travel.html +++ b/app/www/templates/travel.html @@ -32,14 +32,16 @@ {{travel.telegram}}

-

- Ask to join - Offer car - Offer car -

-

- Delete -

+
+

+ Ask to join + Offer car + Offer car +

+

+ Delete +

+
diff --git a/app/www/templates/travels.html b/app/www/templates/travels.html index fff5c0c..4b48c07 100644 --- a/app/www/templates/travels.html +++ b/app/www/templates/travels.html @@ -5,7 +5,7 @@ on-refresh="doRefresh()">
-
+