blob: cd943f9e7aeed0d82a351e05c3c9c66f64c96b23 [file] [log] [blame]
Iftekharul Islam99d199f2017-03-24 15:28:25 -05001/**
2 * userModel
3 *
4 * @module app/common/services/userModel
5 * @exports userModel
6 * @name userModel
7
Iftekharul Islam99d199f2017-03-24 15:28:25 -05008 */
9
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070010window.angular && (function(angular) {
11 'use strict';
Iftekharul Islam99d199f2017-03-24 15:28:25 -050012
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070013 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 Islam99d199f2017-03-24 15:28:25 -050064
Ed Tanousbbcf6702017-10-06 13:53:06 -070065})(window.angular);