This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
configService.service.js
executable file
·86 lines (73 loc) · 2.24 KB
/
configService.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(function () {
'use strict';
angular.module('frontend.core.services')
.factory('configService', configService);
configService.$inject = ['$http', 'Config', 'messageService', '$sessionStorage', '$q', '$state', 'authService'];
function configService($http, Config, messageService, $sessionStorage, $q, $state, authService) {
var config = {};
var firstLoad = true;
var service = {
getConfig: getConfig,
getApp: getApp
};
return service;
// Private functions.
/**
* Return the private `config` object.
* @returns {{}}
*/
function getConfig() {
return config;
}
/**
* Gey the config of the app from its API.
*
* @param {Boolean} ignoreError
* @returns {*}
*/
function getApp(ignoreError) {
// Init promise.
var deferred = $q.defer();
// Get the config of the app.
$http({
method: 'GET',
url: Config.backendUrl + '/admin/config'
}).then(function (response) {
// Set the config.
config = response.data;
// Set the isNewApp value in configService object.
config.isNewApp = response.data.settings && response.data.settings.isNewApp;
config.models = response.data.models;
// Check if the user is connected.
if (!config.connected) {
if (config.isNewApp) {
$state.go('auth.register');
} else if ($state.includes('strapi')) {
$state.go('auth.login');
}
}
deferred.resolve();
})
.catch(function (response) {
if (response.data && response.data.message) {
// User is not admin.
$state.go('auth.login');
if (!ignoreError) {
messageService.error(response.data && response.data.message, 'Error', {
timeOut: 60000
});
}
authService.logout();
} else if (firstLoad) {
// App is offline.
messageService.error('Your app looks offline, please start it and reload this page.', 'Error', {
timeOut: 60000
});
}
firstLoad = false;
deferred.reject();
});
return deferred.promise;
}
}
})();