From f696d33b645c53369ec5833593bdc77c9fb7cea9 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 9 Apr 2024 19:05:28 +0100 Subject: fix: Delete the API key on logout from phone or extension --- apps/mobile/app/signin.tsx | 2 +- apps/mobile/lib/providers.tsx | 51 +++++-------------------------------------- apps/mobile/lib/session.ts | 8 ++++++- apps/mobile/lib/settings.ts | 1 + 4 files changed, 14 insertions(+), 48 deletions(-) (limited to 'apps/mobile') diff --git a/apps/mobile/app/signin.tsx b/apps/mobile/app/signin.tsx index 283528df..80a5f219 100644 --- a/apps/mobile/app/signin.tsx +++ b/apps/mobile/app/signin.tsx @@ -22,7 +22,7 @@ export default function Signin() { const { mutate: login, isPending } = api.apiKeys.exchange.useMutation({ onSuccess: (resp) => { - setSettings({ ...settings, apiKey: resp.key }); + setSettings({ ...settings, apiKey: resp.key, apiKeyId: resp.id }); }, onError: (e) => { if (e.data?.code === "UNAUTHORIZED") { diff --git a/apps/mobile/lib/providers.tsx b/apps/mobile/lib/providers.tsx index ed04b9bf..c2a573f5 100644 --- a/apps/mobile/lib/providers.tsx +++ b/apps/mobile/lib/providers.tsx @@ -1,52 +1,11 @@ -import { useEffect, useMemo } from "react"; +import { useEffect } from "react"; import { SafeAreaProvider } from "react-native-safe-area-context"; import FullPageSpinner from "@/components/ui/FullPageSpinner"; import { ToastProvider } from "@/components/ui/Toast"; -import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; -import { httpBatchLink } from "@trpc/client"; -import superjson from "superjson"; -import type { Settings } from "./settings"; -import useAppSettings from "./settings"; -import { api } from "./trpc"; - -function getTRPCClient(settings: Settings) { - return api.createClient({ - links: [ - httpBatchLink({ - url: `${settings.address}/api/trpc`, - headers() { - return { - Authorization: settings?.apiKey - ? `Bearer ${settings.apiKey}` - : undefined, - }; - }, - transformer: superjson, - }), - ], - }); -} - -function TrpcProvider({ - children, - settings, -}: { - settings: Settings; - children: React.ReactNode; -}) { - const queryClient = useMemo(() => new QueryClient(), [settings]); - - const trpcClient = useMemo(() => getTRPCClient(settings), [settings]); +import { TRPCProvider } from "@hoarder/shared-react/providers/trpc-provider"; - return ( - - - {children} - - - ); -} +import useAppSettings from "./settings"; export function Providers({ children }: { children: React.ReactNode }) { const { settings, isLoading, load } = useAppSettings(); @@ -62,9 +21,9 @@ export function Providers({ children }: { children: React.ReactNode }) { return ( - + {children} - + ); } diff --git a/apps/mobile/lib/session.ts b/apps/mobile/lib/session.ts index bafb3a09..8eb646cb 100644 --- a/apps/mobile/lib/session.ts +++ b/apps/mobile/lib/session.ts @@ -1,12 +1,18 @@ import { useCallback } from "react"; import useAppSettings from "./settings"; +import { api } from "./trpc"; export function useSession() { const { settings, setSettings } = useAppSettings(); + const { mutate: deleteKey } = api.apiKeys.revoke.useMutation(); + const logout = useCallback(() => { - setSettings({ ...settings, apiKey: undefined }); + if (settings.apiKeyId) { + deleteKey({ id: settings.apiKeyId }); + } + setSettings({ ...settings, apiKey: undefined, apiKeyId: undefined }); }, [settings, setSettings]); return { diff --git a/apps/mobile/lib/settings.ts b/apps/mobile/lib/settings.ts index 3ec53231..efb5593a 100644 --- a/apps/mobile/lib/settings.ts +++ b/apps/mobile/lib/settings.ts @@ -5,6 +5,7 @@ const SETTING_NAME = "settings"; export interface Settings { apiKey?: string; + apiKeyId?: string; address: string; } -- cgit v1.2.3-70-g09d2