Skip to content

Commit

Permalink
Get token from local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Jun 18, 2024
1 parent fae02c6 commit 9d11f15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"version": "1.8.1",
"manifest_version": 3,
"permissions": [
"webRequest"
"webRequest",
"storage"
],
"host_permissions": [
"*://*.timechimp.com/*"
Expand Down
13 changes: 12 additions & 1 deletion src/TimeChimpApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { getTokenFromStorage } from './content/storage';

export class TimeChimpApi {
private async doFetch<T>(path: string): Promise<T> {
const token = getTokenFromStorage();
if (!token) {
throw new Error('No token found in local storage');
}

const url = `https://web.timechimp.com${path}`;
const response = await fetch(url);
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
const body = await response.text();

if (response.status >= 400) {
Expand Down
13 changes: 13 additions & 0 deletions src/content/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export function getTokenFromStorage(): string | null {
for (let i = 0; i < window.localStorage.length; i++) {
const key = window.localStorage.key(i);
if (
key &&
key.startsWith('CognitoIdentityServiceProvider.') &&
key.endsWith('.accessToken')
) {
return window.localStorage.getItem(key);
}
}
return null;
}

0 comments on commit 9d11f15

Please sign in to comment.