blob: e25d3f9536865eeaad87cdc5dfd65aec704b30ea [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
10window.angular && (function (angular) {
11 'use strict';
12
13 angular
14 .module('app.common.services')
15 .service('userModel', ['APIUtils',function(APIUtils){
16 return {
Iftekharul Islam99d199f2017-03-24 15:28:25 -050017 login : function(username, password, callback){
18 APIUtils.login(username, password, function(response, error){
Ed Tanousbbcf6702017-10-06 13:53:06 -070019 if(response &&
20 (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS
21 || response.status === undefined)){
Iftekharul Islam99d199f2017-03-24 15:28:25 -050022 sessionStorage.setItem('LOGIN_ID', username);
23 callback(true);
Alexander Filippov0e480f52018-03-20 10:42:26 +030024 }else if(response && response.data && response.data.data
25 && response.data.data.description){
26 callback(false, response.data.data.description);
27 }else if(response && response.message){
28 callback(false, response.message);
29 }else if(error){
30 callback(false, 'Server unreachable');
Iftekharul Islam99d199f2017-03-24 15:28:25 -050031 }else{
Alexander Filippov0e480f52018-03-20 10:42:26 +030032 callback(false, 'Internal error');
Iftekharul Islam99d199f2017-03-24 15:28:25 -050033 }
34 });
35 },
36 isLoggedIn : function(){
37 if(sessionStorage.getItem('LOGIN_ID') === null){
38 return false;
39 }
40 return true;
41 },
42 logout : function(callback){
43 APIUtils.logout(function(response, error){
44 if(response &&
45 response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS){
46 sessionStorage.removeItem('LOGIN_ID');
Iftekharul Islam1acb4122017-11-02 13:20:32 -050047 sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050048 callback(true);
Iftekharul Islamec6bcd12017-09-06 10:49:07 -050049 }else if(response.status == APIUtils.API_RESPONSE.ERROR_STATUS){
50 callback(false);
Iftekharul Islam99d199f2017-03-24 15:28:25 -050051 }else{
52 callback(false, error);
53 }
54 });
55 }
56 };
57 }]);
58
Ed Tanousbbcf6702017-10-06 13:53:06 -070059})(window.angular);