Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 1 | /** |
| 2 | * userModel |
| 3 | * |
| 4 | * @module app/common/services/userModel |
| 5 | * @exports userModel |
| 6 | * @name userModel |
| 7 | |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 10 | window.angular && (function(angular) { |
| 11 | 'use strict'; |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 12 | |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 13 | angular.module('app.common.services').service('userModel', [ |
| 14 | 'APIUtils', |
| 15 | function(APIUtils) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 16 | return { |
| 17 | login: function(username, password, callback) { |
| 18 | APIUtils.login(username, password, function(response, error) { |
| 19 | if (response && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 20 | (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS || |
| 21 | response.status === undefined)) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 22 | sessionStorage.setItem('LOGIN_ID', username); |
| 23 | callback(true); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 24 | } else if ( |
| 25 | response && response.data && response.data.data && |
| 26 | response.data.data.description) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 27 | callback(false, response.data.data.description); |
beccabroek | b5c5dc5 | 2019-03-21 16:54:19 -0500 | [diff] [blame] | 28 | } else if (response && response.statusText) { |
| 29 | callback(false, response.statusText); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 30 | } else if (error) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 31 | callback(false, 'Server unreachable'); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 32 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 33 | callback(false, 'Internal error'); |
| 34 | } |
| 35 | }); |
| 36 | }, |
| 37 | isLoggedIn: function() { |
| 38 | if (sessionStorage.getItem('LOGIN_ID') === null) { |
| 39 | return false; |
| 40 | } |
| 41 | return true; |
| 42 | }, |
| 43 | logout: function(callback) { |
| 44 | APIUtils.logout(function(response, error) { |
| 45 | if (response && |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 46 | response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 47 | sessionStorage.removeItem('LOGIN_ID'); |
| 48 | sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY); |
| 49 | callback(true); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 50 | } else if (response.status == APIUtils.API_RESPONSE.ERROR_STATUS) { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 51 | callback(false); |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 52 | } else { |
Andrew Geissler | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame] | 53 | callback(false, error); |
| 54 | } |
| 55 | }); |
| 56 | } |
| 57 | }; |
Andrew Geissler | d27bb13 | 2018-05-24 11:07:27 -0700 | [diff] [blame] | 58 | } |
| 59 | ]); |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 60 | })(window.angular); |