summaryrefslogtreecommitdiffstats
path: root/app/common/services/userModel.js
blob: f73c7a858c57856c634da791272dc64a7637a55d (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
59
60
/**
 * userModel
 *
 * @module app/common/services/userModel
 * @exports userModel
 * @name userModel

 */

window.angular && (function(angular) {
  'use strict';

  angular.module('app.common.services').service('userModel', [
    'APIUtils',
    function(APIUtils) {
      return {
        login: function(username, password, callback) {
          APIUtils.login(username, password, function(response, error) {
            if (response &&
                (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS ||
                 response.status === undefined)) {
              sessionStorage.setItem('LOGIN_ID', username);
              callback(true);
            } else if (
                response && response.data && response.data.data &&
                response.data.data.description) {
              callback(false, response.data.data.description);
            } else if (response && response.statusText) {
              callback(false, response.statusText);
            } else if (error) {
              callback(false, 'Server unreachable');
            } else {
              callback(false, 'Internal error');
            }
          });
        },
        isLoggedIn: function() {
          if (sessionStorage.getItem('LOGIN_ID') === null) {
            return false;
          }
          return true;
        },
        logout: function(callback) {
          APIUtils.logout(function(response, error) {
            if (response &&
                response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS) {
              sessionStorage.removeItem('LOGIN_ID');
              sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
              callback(true);
            } else if (response.status == APIUtils.API_RESPONSE.ERROR_STATUS) {
              callback(false);
            } else {
              callback(false, error);
            }
          });
        }
      };
    }
  ]);
})(window.angular);
OpenPOWER on IntegriCloud