Added authentication to Pi-Hole widgets

This commit is contained in:
Alberto Caravaca
2023-01-20 15:02:55 -06:00
parent c957ab6cfe
commit 5cfc9b1bd8
3 changed files with 26 additions and 8 deletions

View File

@@ -40,8 +40,12 @@ export default {
if (!usersChoice) this.error('You must specify the hostname for your Pi-Hole server');
return usersChoice || 'http://pi.hole';
},
apiKey() {
if (!this.options.apiKey) this.error('API Key is required, please see the docs');
return this.options.apiKey;
},
endpoint() {
return `${this.hostname}/admin/api.php`;
return `${this.hostname}/admin/api.php?summary&auth=${this.apiKey}`;
},
hideStatus() { return this.options.hideStatus; },
hideChart() { return this.options.hideChart; },
@@ -57,7 +61,11 @@ export default {
fetchData() {
this.makeRequest(this.endpoint)
.then((response) => {
this.processData(response);
if (Array.isArray(response)) {
this.error('Got success, but found no results, possible authorization error');
} else {
this.processData(response);
}
});
},
/* Assign data variables to the returned data */

View File

@@ -23,8 +23,12 @@ export default {
if (!usersChoice) this.error('You must specify the hostname for your Pi-Hole server');
return usersChoice || 'http://pi.hole';
},
apiKey() {
if (!this.options.apiKey) this.error('API Key is required, please see the docs');
return this.options.apiKey;
},
endpoint() {
return `${this.hostname}/admin/api.php?overTimeData10mins`;
return `${this.hostname}/admin/api.php?overTimeData10mins&auth=${this.apiKey}`;
},
},
methods: {
@@ -38,7 +42,9 @@ export default {
});
},
validate(response) {
if (!response.ads_over_time || !response.domains_over_time) {
if (Array.isArray(response)) {
this.error('Got success, but found no results, possible authorization error');
} else if (!response.ads_over_time || !response.domains_over_time) {
this.error('Expected data was not returned from Pi-Hole');
return false;
} else if (response.ads_over_time.length < 1) {