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 | ba5e3f3 | 2018-05-24 10:58:00 -0700 | [diff] [blame^] | 13 | angular |
| 14 | .module('app.common.services') |
| 15 | .service('userModel', ['APIUtils', function(APIUtils) { |
| 16 | return { |
| 17 | login: function(username, password, callback) { |
| 18 | APIUtils.login(username, password, function(response, error) { |
| 19 | if (response && |
| 20 | (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS || |
| 21 | response.status === undefined)) { |
| 22 | sessionStorage.setItem('LOGIN_ID', username); |
| 23 | callback(true); |
| 24 | } |
| 25 | else if (response && response.data && response.data.data && |
| 26 | response.data.data.description) { |
| 27 | callback(false, response.data.data.description); |
| 28 | } |
| 29 | else if (response && response.message) { |
| 30 | callback(false, response.message); |
| 31 | } |
| 32 | else if (error) { |
| 33 | callback(false, 'Server unreachable'); |
| 34 | } |
| 35 | else { |
| 36 | callback(false, 'Internal error'); |
| 37 | } |
| 38 | }); |
| 39 | }, |
| 40 | isLoggedIn: function() { |
| 41 | if (sessionStorage.getItem('LOGIN_ID') === null) { |
| 42 | return false; |
| 43 | } |
| 44 | return true; |
| 45 | }, |
| 46 | logout: function(callback) { |
| 47 | APIUtils.logout(function(response, error) { |
| 48 | if (response && |
| 49 | response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS) { |
| 50 | sessionStorage.removeItem('LOGIN_ID'); |
| 51 | sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY); |
| 52 | callback(true); |
| 53 | } |
| 54 | else if (response.status == APIUtils.API_RESPONSE.ERROR_STATUS) { |
| 55 | callback(false); |
| 56 | } |
| 57 | else { |
| 58 | callback(false, error); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | }; |
| 63 | }]); |
Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame] | 64 | |
Ed Tanous | bbcf670 | 2017-10-06 13:53:06 -0700 | [diff] [blame] | 65 | })(window.angular); |