import { Button, Typography } from "@carrot-kpi/ui"; import React, { useCallback, useEffect, useState } from "react"; import LogoIcon from "../icons/logo-icon"; import { t } from "i18next"; import { useAccount, useSignMessage } from "wagmi"; import { useSetDataManagerJWT } from "../hooks/useSetDataManagerJWT"; import { WalletDisconnected } from "./wallet-disconnected"; import { SERVICE_URLS } from "@carrot-kpi/sdk"; interface AuthenticateProps { onCancel: () => void; } export const Authenticate = ({ onCancel }: AuthenticateProps) => { const { address } = useAccount(); const setDataManagerJWT = useSetDataManagerJWT(); const [loading, setLoading] = useState(false); const { data: signedLoginMessage, signMessage } = useSignMessage(); useEffect(() => { const fetchData = async () => { if (!address || !signedLoginMessage) return; setLoading(true); try { const response = await fetch( `${SERVICE_URLS[__ENVIRONMENT__].dataManager}/token`, { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json", }, body: JSON.stringify({ address, signature: signedLoginMessage, }), }, ); if (!response.ok) throw new Error(await response.text()); const { token } = (await response.json()) as { token: string }; setDataManagerJWT(token); setLoading(false); } catch (error) { console.error( "could not get jwt from signed login message", error, ); setLoading(false); } }; fetchData(); }, [address, setDataManagerJWT, signedLoginMessage]); const handleSign = useCallback(() => { const fetchSignableMessage = async () => { if (!address) return; setLoading(true); try { const response = await fetch( `${SERVICE_URLS[__ENVIRONMENT__].dataManager}/login-message/${address}`, ); if (!response.ok) throw new Error(await response.text()); const { message } = (await response.json()) as { message: string; }; signMessage({ message }); } catch (error) { console.error( `could not get and sign the login message for address ${address}`, error, ); setLoading(false); } }; fetchSignableMessage(); }, [address, signMessage]); return (
{address ? ( <> {t("authenticate.title")} {t("authenticate.summary")}
) : ( )}
); };