From 00209ec96f7ceb9609dbe8a26b9cbe6a275bc425 Mon Sep 17 00:00:00 2001 From: Sebastian Velez Date: Wed, 15 Feb 2023 12:25:19 -0500 Subject: [PATCH] chore: get api key from coookie --- src/core/WakaTimeCore.ts | 18 +++++++++++++++++- src/manifests/chrome.json | 2 +- src/types/user.ts | 8 ++++++++ src/utils/checkCurrentUser.ts | 4 ++-- src/utils/user.ts | 7 +++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts index 9109acd..13948e4 100644 --- a/src/core/WakaTimeCore.ts +++ b/src/core/WakaTimeCore.ts @@ -4,7 +4,7 @@ import axios, { AxiosResponse } from 'axios'; import moment from 'moment'; import browser, { Tabs } from 'webextension-polyfill'; import { IDBPDatabase, openDB } from 'idb'; -import { AxiosUserResponse, User } from '../types/user'; +import { ApiKeyPayload, AxiosUserResponse, User } from '../types/user'; import config from '../config/config'; import { SummariesPayload, GrandTotal } from '../types/summaries'; import changeExtensionState from '../utils/changeExtensionState'; @@ -63,6 +63,22 @@ class WakaTimeCore { return summariesAxiosPayload.data.data[0].grand_total; } + /** + * Fetches the api token for logged users in wakatime website + * + * @returns {*} + */ + async fetchApiKey(): Promise { + try { + const apiKeyResponse: AxiosResponse = await axios.post( + `${config.currentUserApiUrl}/get_api_key`, + ); + return apiKeyResponse.data.data.api_key; + } catch (err: unknown) { + return ''; + } + } + /** * Checks if the user is logged in. * diff --git a/src/manifests/chrome.json b/src/manifests/chrome.json index f0abfe5..93b9435 100644 --- a/src/manifests/chrome.json +++ b/src/manifests/chrome.json @@ -26,5 +26,5 @@ "page": "options.html" }, "permissions": ["alarms", "tabs", "storage", "idle"], - "version": "3.0.1" + "version": "3.0.2" } diff --git a/src/types/user.ts b/src/types/user.ts index 4c93212..c5edc51 100644 --- a/src/types/user.ts +++ b/src/types/user.ts @@ -52,3 +52,11 @@ export interface CurrentUser { pending?: boolean; user?: User; } + +export interface ApiKeyPayload { + data: ApiKey; +} + +export interface ApiKey { + api_key: string; +} diff --git a/src/utils/checkCurrentUser.ts b/src/utils/checkCurrentUser.ts index 186c1f9..365e054 100644 --- a/src/utils/checkCurrentUser.ts +++ b/src/utils/checkCurrentUser.ts @@ -8,8 +8,8 @@ export default (store: Store) => (time: number): unsub => { const fetchUser = () => { const apiKey: string = (store.getState() as ReduxSelector).config.apiKey; - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error + + // @ts-expect-error be able to dispatch async thunk store.dispatch(fetchCurrentUser(apiKey)); }; fetchUser(); diff --git a/src/utils/user.ts b/src/utils/user.ts index e5c8c20..1eac805 100644 --- a/src/utils/user.ts +++ b/src/utils/user.ts @@ -34,6 +34,13 @@ export const fetchUserData = async ( apiKey: config.apiKey, }); apiKey = storage.apiKey as string; + if (!apiKey) { + apiKey = await WakaTimeCore.fetchApiKey(); + if (apiKey) { + await browser.storage.sync.set({ apiKey }); + } + } + dispatch(setApiKey(apiKey)); }