blob: f73c7a858c57856c634da791272dc64a7637a55d [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 Geisslerd27bb132018-05-24 11:07:27 -070013 angular.module('app.common.services').service('userModel', [
14 'APIUtils',
15 function(APIUtils) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070016 return {
17 login: function(username, password, callback) {
18 APIUtils.login(username, password, function(response, error) {
19 if (response &&
Andrew Geisslerd27bb132018-05-24 11:07:27 -070020 (response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS ||
21 response.status === undefined)) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070022 sessionStorage.setItem('LOGIN_ID', username);
23 callback(true);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070024 } else if (
25 response && response.data && response.data.data &&
26 response.data.data.description) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070027 callback(false, response.data.data.description);
beccabroekb5c5dc52019-03-21 16:54:19 -050028 } else if (response && response.statusText) {
29 callback(false, response.statusText);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070030 } else if (error) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070031 callback(false, 'Server unreachable');
Andrew Geisslerd27bb132018-05-24 11:07:27 -070032 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070033 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 Geisslerd27bb132018-05-24 11:07:27 -070046 response.status == APIUtils.API_RESPONSE.SUCCESS_STATUS) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070047 sessionStorage.removeItem('LOGIN_ID');
48 sessionStorage.removeItem(APIUtils.HOST_SESSION_STORAGE_KEY);
49 callback(true);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070050 } else if (response.status == APIUtils.API_RESPONSE.ERROR_STATUS) {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070051 callback(false);
Andrew Geisslerd27bb132018-05-24 11:07:27 -070052 } else {
Andrew Geisslerba5e3f32018-05-24 10:58:00 -070053 callback(false, error);
54 }
55 });
56 }
57 };
Andrew Geisslerd27bb132018-05-24 11:07:27 -070058 }
59 ]);
Ed Tanousbbcf6702017-10-06 13:53:06 -070060})(window.angular);