diff --git a/src/components/Apikey.vue b/src/components/Apikey.vue index eb195a2..9c61cec 100644 --- a/src/components/Apikey.vue +++ b/src/components/Apikey.vue @@ -4,8 +4,8 @@

API Key

Your API key is: {{ apiKey }}

+ - diff --git a/src/firebase/data.ts b/src/firebase/data.ts index 802b708..e7c633c 100644 --- a/src/firebase/data.ts +++ b/src/firebase/data.ts @@ -17,8 +17,11 @@ import { query, where } from 'firebase/firestore/lite' +import { getFunctions, httpsCallable } from "firebase/functions"; import type { ScreenTimeData, ScreenTimeSummary } from '@/types' +const functions = getFunctions(); + export function dataToSummary(data: ScreenTimeData): ScreenTimeSummary { const total = data.events.reduce((acc, event) => acc + event.duration, 0) const categoryTotals: { [key: string]: number } = {} @@ -121,3 +124,20 @@ export async function getApiKey(userId: string): Promise { return null } } + +interface ApiResponse { + apiKey: string; +} + +export async function rotateKey(userId: string): Promise { + // invoke a callable function to rotate the user's api key + // this function is defined in functions/src/index.ts + const rotateApiKey = httpsCallable(functions, 'rotateApiKey') + const key = rotateApiKey({ userId: userId }).then((result) => { + return (result.data as ApiResponse)?.apiKey; + }).catch((error) => { + console.error(error) + return null + }) + return key +} diff --git a/src/stores/apikey.ts b/src/stores/apikey.ts index 68c6baf..f67c344 100644 --- a/src/stores/apikey.ts +++ b/src/stores/apikey.ts @@ -1,5 +1,5 @@ import { defineStore } from 'pinia' -import { getApiKey } from '@/firebase/data' +import { getApiKey, rotateKey as rotateKeyCallable } from '@/firebase/data' import { useAuthStore } from './auth' import { ref } from 'vue' @@ -12,7 +12,16 @@ export const useApiKeyStore = defineStore('apikey', () => { apikey.value = key }) } - return { apikey, fetchKey } + function rotateKey() { + const userId = useAuthStore().user!.uid + rotateKeyCallable(userId).then((key) => { + console.log('Key is now', key) + if (key != null) { + apikey.value = key + } + }) + } + return { apikey, fetchKey, rotateKey } }, { persist: true