Skip to content

Commit

Permalink
Fix get users API call error for non-admins
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Oct 31, 2023
1 parent e07fe23 commit c98a04a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "TimeChimp Billability Chart",
"description": "Adds a billability chart on the TimeChimp hours page.",
"version": "1.0.0",
"version": "1.0.1",
"manifest_version": 3,
"permissions": [
"cookies",
Expand Down
24 changes: 18 additions & 6 deletions src/background/timechimp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,33 @@ chrome.cookies.onChanged.addListener((changeInfo) => {
*/
chrome.webRequest.onCompleted.addListener(
async (request) => {
// Gets the id based on the userName
async function getUserId(userName: string) {
const users = await api.getUsers();
return users.find((u) => u.userName === userName)!.id;
/**
* Gets the user id based on the username.
* Note that this is an admin-only endpoint,
* so this will return undefined if called by an unauthorized user.
*/
async function tryGetUserId(userName: string) {
try {
const users = await api.getUsers();
return users.find((u) => u.userName === userName)!.id;
} catch (e) {
return undefined;
}
}

// Get the date and the user email from the request to TimeChimp
const matches = request.url.match('.*/time/week/([^/]+)/(.*)');
if (matches?.length == 3) {
let event = 'weekChanged';
const userIdNew = await getUserId(decodeURIComponent(matches[1]));

const userIdNew = await tryGetUserId(
decodeURIComponent(matches[1]),
);
const userIdFromStorage = await chrome.storage.local.get([
'timeChimpUserId',
]);
if (userIdFromStorage.timeChimpUserId != userIdNew) {

if (userIdNew && userIdFromStorage.timeChimpUserId != userIdNew) {
console.debug('Switching to user ' + userIdNew);
event = 'userChanged';
await chrome.storage.local.set({ timeChimpUserId: userIdNew });
Expand Down

0 comments on commit c98a04a

Please sign in to comment.