Skip to content

Commit

Permalink
fix: Delete the API key on logout from phone or extension
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Apr 9, 2024
1 parent 5ab6c33 commit f696d33
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 92 deletions.
7 changes: 6 additions & 1 deletion apps/browser-extension/src/OptionsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default function OptionsPage() {
},
);

const { mutate: deleteKey } = api.apiKeys.revoke.useMutation();

const invalidateWhoami = api.useUtils().users.whoami.refetch;

useEffect(() => {
Expand All @@ -39,7 +41,10 @@ export default function OptionsPage() {
}

const onLogout = () => {
setSettings((s) => ({ ...s, apiKey: "" }));
if (settings.apiKeyId) {
deleteKey({ id: settings.apiKeyId });
}
setSettings((s) => ({ ...s, apiKey: "", apiKeyId: undefined }));
invalidateWhoami();
navigate("/notconfigured");
};
Expand Down
2 changes: 1 addition & 1 deletion apps/browser-extension/src/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function SignInPage() {
isPending,
} = api.apiKeys.exchange.useMutation({
onSuccess: (resp) => {
setSettings((s) => ({ ...s, apiKey: resp.key }));
setSettings((s) => ({ ...s, apiKey: resp.key, apiKeyId: resp.id }));
navigate("/options");
},
});
Expand Down
44 changes: 3 additions & 41 deletions apps/browser-extension/src/utils/providers.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,9 @@
import { useEffect, useState } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { httpBatchLink } from "@trpc/client";
import superjson from "superjson";
import { TRPCProvider } from "@hoarder/shared-react/providers/trpc-provider";

import usePluginSettings, { getPluginSettings } from "./settings";
import { api } from "./trpc";

function getTRPCClient(address: string) {
return api.createClient({
links: [
httpBatchLink({
url: `${address}/api/trpc`,
async headers() {
const settings = await getPluginSettings();
return {
Authorization: `Bearer ${settings.apiKey}`,
};
},
transformer: superjson,
}),
],
});
}
import usePluginSettings from "./settings";

export function Providers({ children }: { children: React.ReactNode }) {
const { settings } = usePluginSettings();
const [queryClient] = useState(() => new QueryClient());

const [trpcClient, setTrpcClient] = useState<
ReturnType<typeof getTRPCClient>
>(getTRPCClient(settings.address));

useEffect(() => {
setTrpcClient(getTRPCClient(settings.address));
}, [settings.address]);

return (
<api.Provider
key={settings.address}
client={trpcClient}
queryClient={queryClient}
>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</api.Provider>
);
return <TRPCProvider settings={settings}>{children}</TRPCProvider>;
}
1 change: 1 addition & 0 deletions apps/browser-extension/src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useChromeStorageSync } from "use-chrome-storage";

export interface Settings {
apiKey: string;
apiKeyId?: string;
address: string;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/app/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
51 changes: 5 additions & 46 deletions apps/mobile/lib/providers.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<api.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>
<ToastProvider>{children}</ToastProvider>
</QueryClientProvider>
</api.Provider>
);
}
import useAppSettings from "./settings";

export function Providers({ children }: { children: React.ReactNode }) {
const { settings, isLoading, load } = useAppSettings();
Expand All @@ -62,9 +21,9 @@ export function Providers({ children }: { children: React.ReactNode }) {

return (
<SafeAreaProvider>
<TrpcProvider settings={settings}>
<TRPCProvider settings={settings}>
<ToastProvider>{children}</ToastProvider>
</TrpcProvider>
</TRPCProvider>
</SafeAreaProvider>
);
}
8 changes: 7 additions & 1 deletion apps/mobile/lib/session.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const SETTING_NAME = "settings";

export interface Settings {
apiKey?: string;
apiKeyId?: string;
address: string;
}

Expand Down
4 changes: 3 additions & 1 deletion packages/shared-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"private": true,
"dependencies": {
"@hoarder/trpc": "workspace:^0.1.0",
"@tanstack/react-query": "^5.24.8"
"@tanstack/react-query": "^5.24.8",
"superjson": "^2.2.1",
"@trpc/client": "11.0.0-next-beta.308"
},
"devDependencies": {
"@hoarder/eslint-config": "workspace:^0.2.0",
Expand Down
50 changes: 50 additions & 0 deletions packages/shared-react/providers/trpc-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useMemo } from "react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { httpBatchLink } from "@trpc/client";
import superjson from "superjson";

import { api } from "../trpc";

interface Settings {
apiKey?: string;
address: string;
}

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,
}),
],
});
}

export function TRPCProvider({
settings,
children,
}: {
settings: Settings;
children: React.ReactNode;
}) {
const queryClient = useMemo(() => new QueryClient(), [settings]);
const trpcClient = useMemo(() => getTRPCClient(settings), [settings]);

return (
<api.Provider
key={settings.address}
client={trpcClient}
queryClient={queryClient}
>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</api.Provider>
);
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f696d33

Please sign in to comment.