blob: 43437c5ab978f39ef466193573eac41a18fd8e96 [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
8 * @version 0.0.1
9 */
10
11window.angular && (function (angular) {
12 'use strict';
13
14 angular
15 .module('app.common.services')
16 .service('userModel', ['APIUtils',function(APIUtils){
17 return {
Iftekharul Islam99d199f2017-03-24 15:28:25 -050018 login : function(username, password, callback){
19 APIUtils.login(username, password, function(response, error){
Ed Tanousbbcf6702017-10-06 13:53:06 -070020 if(response &&
21 (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS
22 || response.status === undefined)){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050023 sessionStorage.setItem('LOGIN_ID', username);
24 callback(true);
Alexander Filippov0e480f52018-03-20 10:42:26 +030025 }else if(response && response.data && response.data.data
26 && response.data.data.description){
27 callback(false, response.data.data.description);
28 }else if(response && response.message){
29 callback(false, response.message);
30 }else if(error){
31 callback(false, 'Server unreachable');
Iftekharul Islam99d199f2017-03-24 15:28:25 -050032 }else{
Alexander Filippov0e480f52018-03-20 10:42:26 +030033 callback(false, 'Internal error');
Iftekharul Islam99d199f2017-03-24 15:28:25 -050034 }
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 &&
46 response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){
47 sessionStorage.removeItem('LOGIN_ID');
Iftekharul Islam1acb4122017-11-02 13:20:32 -050048 sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050049 callback(true);
Iftekharul Islamec6bcd12017-09-06 10:49:07 -050050 }else if(response.status == APIUtils.API_RESPONSE.ERROR_STATUS){
51 callback(false);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050052 }else{
53 callback(false, error);
54 }
55 });
56 }
57 };
58 }]);
59
Ed Tanousbbcf6702017-10-06 13:53:06 -070060})(window.angular);