authentication.controller.js 2.02 KB
(function(){
	'use strict';

		angular.module('sbAdminApp')
			.controller('LoginCtrl', ['$scope', '$rootScope', '$state','$mdDialog','$log', 'AUTH_EVENTS', 'LoginService', 'toaster', loginCtrl]);

		function loginCtrl($scope, $rootScope, $state, $mdDialog,$log, AUTH_EVENTS, LoginService ,toaster) {
			$scope.doLogin = function() {
				$rootScope.auth = window.btoa($scope.loginData.email + ':' + $scope.loginData.password);
		        LoginService.doLogin().save($scope.loginData).$promise.then(function(result) {
		        	console.log("result",result);
		            if(result){
				       	$rootScope.$broadcast(AUTH_EVENTS.updateUser);
	                    LoginService.setUser(result);
	                    /*if(result.role === 'admin'){
				        	$state.go("app.user");
	                    } else {
	                    	$state.go("app.");
	                    } */
	                    $state.go("app.user");
					} 
					if (Notification.permission !== 'denied') {
				        Notification.requestPermission(function(permission) {
				          // If the user accepts, let's create a notification
			                if (permission === "granted") {
			                  /*var notification = new Notification("Hello!");*/
			                }
				        });
				    }
				},
			    function error(error) {
			    	console.log("ererrorror",error);
			        if(error){
			        	toaster.error("server error");
			        }
			    });

			}
			$scope.signUp=function(){

				LoginService.signUp().save($scope.signupData).$promise.then(function(result) {
					console.log("result",result);
					// toaster.success("signed up successfully")
					 $mdDialog.show(
                        $mdDialog.alert()
                           .clickOutsideToClose(true)
                           .title()
                           .textContent("Please check your email and click on the verification link before logging in.")
                           .ariaLabel('Alert')
                           .ok('ok')
                       );

				});

				
			}
			
		}
})();