Skip to content

Instantly share code, notes, and snippets.

@vitaliy-bobrov
Created February 16, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitaliy-bobrov/6f14a26067647f90b379 to your computer and use it in GitHub Desktop.
Save vitaliy-bobrov/6f14a26067647f90b379 to your computer and use it in GitHub Desktop.
Tutorial App part 5 - controller.js
.controller('LoginCtrl', ['$rootScope', '$scope', '$ionicLoading', '$ionicModal', 'storage', 'config', 'User', function($rootScope, $scope, $ionicLoading, $ionicModal, storage, config, User) {
$scope.loginData = {};
$scope.loginError = false;
$rootScope.token = storage.get(config.localStoragePrefix + 'token') || false;
$rootScope.userData = storage.get(config.localStoragePrefix + 'user') || {};
$rootScope.loginSuccess = ($rootScope.token) ? true : false;
// Init ionicModal
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
$scope.closeLogin = function() {
$scope.modal.hide();
};
$rootScope.showLogin = function() {
$scope.modal.show();
};
$rootScope.doLogout = function() {
$ionicLoading.show();
User.logout($rootScope.token).then(function() {
$rootScope.loginSuccess = false;
$scope.loginData = {};
$scope.loginError = false;
User.removeUserData();
$ionicLoading.hide();
}, function(error) {
$ionicLoading.hide();
console.log(error);
});
};
$scope.doLogin = function() {
$ionicLoading.show();
User.getSessionToken().then(function(data) {
$rootScope.token = data;
User.login($scope.loginData.username, $scope.loginData.password, $rootScope.token).then(function(data) {
$ionicLoading.hide();
$scope.loginError = false;
$rootScope.loginSuccess = true;
User.setUserData(data.user, data.token);
$scope.modal.hide();
}, function(error) {
$ionicLoading.hide();
$scope.loginError = error;
});
}, function(error) {
$ionicLoading.hide();
console.log(error);
});
};
}])
.controller('RegisterCtrl', ['$rootScope', '$scope', '$ionicLoading', '$ionicModal', 'storage', 'config', 'User', function($rootScope, $scope, $ionicLoading, $ionicModal, storage, config, User) {
$scope.registerData = {};
$scope.registerError = false;
// Init ionicModal
$ionicModal.fromTemplateUrl('templates/register.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
$scope.closeRegister = function() {
$scope.modal.hide();
};
$rootScope.openRegister = function() {
$scope.modal.show();
};
$scope.doRegistration = function() {
$ionicLoading.show();
User.register($scope.registerData).then(function(data) {
$scope.registerError = false;
$scope.closeRegister();
User.getSessionToken().then(function(data) {
$rootScope.token = data;
User.login($scope.registerData.username, $scope.registerData.password, $rootScope.token).then(function(data) {
$ionicLoading.hide();
$rootScope.loginSuccess = true;
User.setUserData(data.user, data.token);
$scope.modal.hide();
}, function(error) {
$ionicLoading.hide();
console.log(error);
});
}, function(error) {
$ionicLoading.hide();
console.log(error);
});
}, function(error) {
$ionicLoading.hide();
$scope.registerError = error.form_errors;
});
}
}])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment