Skip to content

Commit

Permalink
update basic auth
Browse files Browse the repository at this point in the history
Signed-off-by: Diana Barsan <[email protected]>
  • Loading branch information
dianabarsan committed Dec 16, 2024
1 parent e485888 commit acecaf5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
2 changes: 2 additions & 0 deletions scripts/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const getApiUrl = (pathname = '') => {
apiUrl.port = API_PORT || DEFAULT_API_PORT;
apiUrl.pathname = pathname;
const basicAuth = btoa(`${apiUrl.username}:${apiUrl.password}`);
apiUrl.username = '';
apiUrl.password = '';

return {
url: apiUrl.toString(),
Expand Down
25 changes: 14 additions & 11 deletions scripts/set-default-contact/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ const compileUrl = path => {
}
};

const options = {
headers: {
'Authorization': 'Basic ' + btoa(`${url.username}:${url.password}`),
'Content-Type': 'application/json'
}
};

const fetchUrl = async (url, options) => {
const response = await fetch(url.href, options);
options = {
...options,
headers: {
'Authorization': 'Basic ' + btoa(`${url.username}:${url.password}`),
'Content-Type': 'application/json'
}
};
url.username = '';
url.password = '';

const response = await fetch(url.toString(), options);
if (response.ok) {
return await response.json();
}
Expand All @@ -31,7 +34,7 @@ const fetchUrl = async (url, options) => {
const getChtUsers = async () => {
// pin to v1 of API so it is backwards compatible with CHT 3.x
const url = compileUrl('/api/v1/users');
const users = await fetchUrl(url, options);
const users = await fetchUrl(url);
if (typeof users === 'object') {
console.log(` Found ${users.length} users\n`);
return users;
Expand All @@ -40,7 +43,7 @@ const getChtUsers = async () => {

const getObjectFromMedicDb = async id => {
const url = compileUrl('/medic/' + id);
return fetchUrl(url, options);
return fetchUrl(url);
};

const hasDefaultContact = async user => {
Expand Down Expand Up @@ -72,7 +75,7 @@ const savePlace = async (placeId, contactId) => {
}
placeObj.contact._id = contactId;
placeObj.contact.parent = {_id: placeId};
return fetchUrl(url.href, { ...options, body: JSON.stringify(placeObj), method: 'PUT' });
return fetchUrl(url, { body: JSON.stringify(placeObj), method: 'PUT' });
};


Expand Down

0 comments on commit acecaf5

Please sign in to comment.