auth.controller.js 1.34 KB
(function () {
  'use strict';

  angular
    .module('dialoga')
    .controller('AuthPageController', AuthPageController);

  /** @ngInject */
  function AuthPageController($scope, $rootScope, AUTH_EVENTS, AuthService, Session, $log) {
    $log.debug('AuthPageController');

    var vm = this;

    vm.$rootScope = $rootScope;
    vm.$scope = $scope;
    vm.AUTH_EVENTS = AUTH_EVENTS;
    vm.AuthService = AuthService;
    vm.Session = Session;
    vm.$log = $log;

    vm.init();
  }

  AuthPageController.prototype.init = function() {
    var vm = this;

    // init variables
    vm.credentials = {};

    // attach events
    vm.currentUser = vm.Session.getCurrentUser();

      // handle login
      vm.$scope.$on(vm.AUTH_EVENTS.loginSuccess, function () {
        vm.currentUser = vm.Session.getCurrentUser();
      });

      // handle logout
      vm.$scope.$on(vm.AUTH_EVENTS.logoutSuccess, function () {
        vm.currentUser = vm.Session.getCurrentUser();
      });
    // ...
  };


  AuthPageController.prototype.onClickLogout = function (){
    var vm = this;

    vm.AuthService.logout();
  };

  AuthPageController.prototype.login = function(credentials) {
    var vm = this;

    vm.AuthService.login(credentials).then(function(user) {
      // handle view
      vm.$log.debug('user', user);
    }, function() {
      // handle view
    });
  };

})();