Compare commits

...

6 Commits

Author SHA1 Message Date
Rajiv Sahal d3630abf9c
Merge branch 'main' into validate-cal-provider-keys 2023-11-27 22:02:00 +05:30
Rajiv Sahal 094930363c
Merge branch 'main' into validate-cal-provider-keys 2023-11-27 17:29:37 +05:30
Ryukemeister 01c4aee3ec replace v1 with v2 2023-11-21 13:53:33 +05:30
Ryukemeister 0e9d6b3aeb Merge branch 'main' into validate-cal-provider-keys 2023-11-21 13:51:53 +05:30
Ryukemeister 5454105c36 remove localhost from api call 2023-11-21 13:51:35 +05:30
Ryukemeister 4d4be710d8 validate api keys in cal provider 2023-11-20 12:39:11 +05:30

View File

@ -1,5 +1,5 @@
import type { ReactNode } from "react";
import { createContext, useContext } from "react";
import { createContext, useContext, useState, useEffect } from "react";
type CalProviderProps = {
apiKey: string;
@ -11,5 +11,30 @@ const ApiKeyContext = createContext("");
export const useApiKey = () => useContext(ApiKeyContext);
export function CalProvider({ apiKey, children }: CalProviderProps) {
return <ApiKeyContext.Provider value={apiKey}>{children}</ApiKeyContext.Provider>;
const [key, setKey] = useState("");
useEffect(() => {
async function verifyKey(key: string) {
try {
// here we'll call the /me endpoint in v2 to get user profile
// v2 is not ready yet though
const response = await fetch(`/v2/me?apiKey=${key}`);
if (response.ok) {
setKey(apiKey);
}
} catch (error) {
console.error(error);
setKey("invalid_key");
}
}
if (apiKey.length === 0) {
setKey("no_key");
} else {
verifyKey(apiKey);
}
}, [apiKey]);
return <ApiKeyContext.Provider value={key}>{children}</ApiKeyContext.Provider>;
}