xref: /openbmc/phosphor-webui/app/common/services/userModel.js (revision f2127efa1b7d1c9fc1625f6e30b75be0ed658d53)
199d199f3SIftekharul Islam/**
299d199f3SIftekharul Islam * userModel
399d199f3SIftekharul Islam *
499d199f3SIftekharul Islam * @module app/common/services/userModel
599d199f3SIftekharul Islam * @exports userModel
699d199f3SIftekharul Islam * @name userModel
799d199f3SIftekharul Islam
899d199f3SIftekharul Islam */
999d199f3SIftekharul Islam
1099d199f3SIftekharul Islamwindow.angular && (function(angular) {
1199d199f3SIftekharul Islam  'use strict';
1299d199f3SIftekharul Islam
13d27bb135SAndrew Geissler  angular.module('app.common.services').service('userModel', [
14*f2127efaSWiktor Gołgowski    '$cookies', 'APIUtils',
15*f2127efaSWiktor Gołgowski    function($cookies, APIUtils) {
1699d199f3SIftekharul Islam      return {
1799d199f3SIftekharul Islam        login: function(username, password, callback) {
1899d199f3SIftekharul Islam          APIUtils.login(username, password, function(response, error) {
1999d199f3SIftekharul Islam            if (response &&
20ba5e3f34SAndrew Geissler                (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS ||
21ba5e3f34SAndrew Geissler                 response.status === undefined)) {
2299d199f3SIftekharul Islam              sessionStorage.setItem('LOGIN_ID', username);
2399d199f3SIftekharul Islam              callback(true);
24d27bb135SAndrew Geissler            } else if (
25d27bb135SAndrew Geissler                response && response.data && response.data.data &&
26ba5e3f34SAndrew Geissler                response.data.data.description) {
270e480f55SAlexander Filippov              callback(false, response.data.data.description);
28b5c5dc5dSbeccabroek            } else if (response && response.statusText) {
29b5c5dc5dSbeccabroek              callback(false, response.statusText);
30d27bb135SAndrew Geissler            } else if (error) {
310e480f55SAlexander Filippov              callback(false, 'Server unreachable');
32d27bb135SAndrew Geissler            } else {
330e480f55SAlexander Filippov              callback(false, 'Internal error');
3499d199f3SIftekharul Islam            }
3599d199f3SIftekharul Islam          });
3699d199f3SIftekharul Islam        },
3799d199f3SIftekharul Islam        isLoggedIn: function() {
38*f2127efaSWiktor Gołgowski          if ((sessionStorage.getItem('LOGIN_ID') === null) &&
39*f2127efaSWiktor Gołgowski              (($cookies.get('IsAuthenticated') === undefined) ||
40*f2127efaSWiktor Gołgowski               ($cookies.get('IsAuthenticated') == 'false'))) {
4199d199f3SIftekharul Islam            return false;
4299d199f3SIftekharul Islam          }
4399d199f3SIftekharul Islam          return true;
4499d199f3SIftekharul Islam        },
4599d199f3SIftekharul Islam        logout: function(callback) {
4699d199f3SIftekharul Islam          APIUtils.logout(function(response, error) {
4799d199f3SIftekharul Islam            if (response &&
4899d199f3SIftekharul Islam                response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS) {
4999d199f3SIftekharul Islam              sessionStorage.removeItem('LOGIN_ID');
501acb412dSIftekharul Islam              sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
51*f2127efaSWiktor Gołgowski              $cookies.remove('IsAuthenticated');
5299d199f3SIftekharul Islam              callback(true);
53d27bb135SAndrew Geissler            } else if (response.status == APIUtils.API_RESPONSE.ERROR_STATUS) {
54ec6bcd10SIftekharul Islam              callback(false);
55d27bb135SAndrew Geissler            } else {
5699d199f3SIftekharul Islam              callback(false, error);
5799d199f3SIftekharul Islam            }
5899d199f3SIftekharul Islam          });
5999d199f3SIftekharul Islam        }
6099d199f3SIftekharul Islam      };
61d27bb135SAndrew Geissler    }
62d27bb135SAndrew Geissler  ]);
6399d199f3SIftekharul Islam})(window.angular);
64