aboutsummaryrefslogtreecommitdiffstats
path: root/old/moon_gui/static/app/authentication/authentication.controller.js
blob: ce38bc5fc33ebedf307982f3bd25afd7ab42d6a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
 * @author Samy Abdallah
 */
(function() {

    'use strict';

    angular
        .module('moon')
        .controller('AuthenticationController', AuthenticationController);

    AuthenticationController.$inject = ['authenticationService', '$translate', 'alertService', '$state', '$rootScope'];

    function AuthenticationController(authenticationService, $translate, alertService, $state, $rootScope) {

        var vm = this;

        vm.login = login;
        vm.loading = false;

        vm.credentials = {
            username : '',
            password : ''
        };

        activate();

        function activate(){
            if($rootScope.connected){
                $state.go('moon.dashboard');
            }
        }

        function login(){
            vm.loading = true;
            authenticationService.Login(vm.credentials, loginSuccess, loginError);
        }

        function loginSuccess() {

            $translate('moon.login.success').then( function(translatedValue) {
                alertService.alertSuccess(translatedValue);
                $state.go('moon.dashboard');
                vm.loading = false;
            });

        }

        function loginError(reason) {

            $translate('moon.login.error', { errorCode: reason.status }).then( function(translatedValue) {
                alertService.alertError(translatedValue);
                vm.loading = false;
            });

        }
    }
})();