mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-01 11:06:02 +01:00
Merge pull request #2278 from DR-Blogs/PiHole-v6-API-reintegration
Update PiHole authentication. (PiHole v6)
This commit is contained in:
@@ -13,25 +13,42 @@ export class PiHoleClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSummary() {
|
async getSummary() {
|
||||||
|
const authResponse = await fetch(`${this.baseHostName}/api/auth`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ password: this.apiToken })
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!authResponse.ok) {
|
||||||
|
throw new Error(`Authentication failed: ${authResponse.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const authJson = await authResponse.json();
|
||||||
|
if (!authJson.session?.valid || !authJson.session.sid) {
|
||||||
|
throw new Error(`Invalid session response: ${JSON.stringify(authJson)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const sessionId = authJson.session.sid;
|
||||||
|
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
new URL(`${this.baseHostName}/admin/api.php?summaryRaw&auth=${this.apiToken}`)
|
new URL(`${this.baseHostName}/api.php?summaryRaw&auth=${sessionId}`)
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
throw new Error(`Status code does not indicate success: ${response.status}`);
|
throw new Error(`Failed to fetch summary: ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
|
|
||||||
if (Array.isArray(json)) {
|
if (Array.isArray(json)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Response does not indicate success. Authentication is most likely invalid: ${json}`
|
`Response does not indicate success. Authentication might be invalid: ${json}`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return json as PiHoleApiSummaryResponse;
|
return json as PiHoleApiSummaryResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async enable() {
|
async enable() {
|
||||||
const response = await this.sendStatusChangeRequest('enable');
|
const response = await this.sendStatusChangeRequest('enable');
|
||||||
return response.status === 'enabled';
|
return response.status === 'enabled';
|
||||||
@@ -48,8 +65,8 @@ export class PiHoleClient {
|
|||||||
): Promise<PiHoleApiStatusChangeResponse> {
|
): Promise<PiHoleApiStatusChangeResponse> {
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
duration !== 0
|
duration !== 0
|
||||||
? `${this.baseHostName}/admin/api.php?${action}=${duration}&auth=${this.apiToken}`
|
? `${this.baseHostName}/api?${action}=${duration}&auth=${this.apiToken}`
|
||||||
: `${this.baseHostName}/admin/api.php?${action}&auth=${this.apiToken}`
|
: `${this.baseHostName}/api?${action}&auth=${this.apiToken}`
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.status !== 200) {
|
if (response.status !== 200) {
|
||||||
|
|||||||
Reference in New Issue
Block a user