Skip to content

Commit

Permalink
Merge pull request #1 from meerkatzenwildschein/basic_auth_support
Browse files Browse the repository at this point in the history
Basic auth support
  • Loading branch information
TheDuffman85 authored Aug 19, 2024
2 parents 95f90dd + aaaf9f7 commit 073d511
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,24 @@ document.addEventListener('DOMContentLoaded', function() {
setTimeout(() => { warning.classList.add('hidden'); }, 3000);
return;
}

const fullUrl = apiUrl + '/' + topic;


const urlObj = new URL(apiUrl);
const username = urlObj.username;
const password = urlObj.password;

// Remove username and password from url
urlObj.username = '';
urlObj.password = '';

const fullUrl = urlObj.toString() + topic;

const headers = new Headers();
if(username && password) {
const credentials = btoa(`${username}:${password}`);
headers.set('Authorization', `Basic ${credentials}`);
}
if (accessToken) {
headers.set('Authorization', 'Bearer ' + accessToken);
headers.set('Authorization', `Bearer ${accessToken}`);
}

fetch(fullUrl, {
Expand All @@ -114,7 +126,7 @@ document.addEventListener('DOMContentLoaded', function() {
body: message
}).then(response => {
if (!response.ok) {
console.error("Failed to send message:", response);
console.error(`Failed to send message: ${response.status}`);
status.textContent = "Failed to send message.";
status.style.color = 'red';
} else {
Expand Down

0 comments on commit 073d511

Please sign in to comment.