angular.module('lilybook').controller('AppController', function ($rootScope, $state, $mdSidenav, $mdToast, menuSvc, userSvc) {

	var self = this;

	var getSimpleToast = function (message) {
		return $mdToast.simple().content(message).position('top right');
	};

	menuSvc.getSideNav().then(function (sidenav) {
		self.sidenav = sidenav;
	});

	userSvc.current().then(function (user) {
		$rootScope.user = user;
	});

	self.toggleSidenav = function (sidenavId) {
		$mdSidenav(sidenavId).toggle();
	};

	self.signup = function (signupData) {
		userSvc.signUp(signupData.email, signupData.password, signupData.firstname, signupData.lastname).then(function (user) {
			$rootScope.user = user;
			$state.go('app.home');
		}, function (error) {
				$mdToast.show(getSimpleToast(error.message));
			});
	};

	self.login = function (loginData) {
		userSvc.logIn(loginData.email, loginData.password).then(function (user) {
			$rootScope.user = user;
			$state.go('app.home');
		}, function (error) {
				$mdToast.show(getSimpleToast(error.message));
			});
	};

	self.logout = function () {
		userSvc.logOut().then(function () {
			$rootScope.user = null;
			$state.go('app.splash');
		});
	};
});
@@ -1176,12 +1176,236 @@
 %09%7D);%0A%09%7D;
+%0A%0A%09$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) %7B%0A%09%09console.log('$stateChangeError', arguments);%0A%09%09if (error === 'AUTH_REQUIRED') %7B%0A%09%09%09$state.go('app.login');%0A%09%09%7D%0A%09%7D);
 %0A%7D);

handle auth error