aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mobile/lib/session.ts
blob: d647014577843e7ab0699f7dab9572c534e89fa1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { useCallback } from "react";
import { useMutation } from "@tanstack/react-query";

import { useTRPC } from "@karakeep/shared-react/trpc";

import useAppSettings from "./settings";

export function useSession() {
  const { settings, setSettings } = useAppSettings();
  const api = useTRPC();

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

  const logout = useCallback(() => {
    if (settings.apiKeyId) {
      deleteKey({ id: settings.apiKeyId });
    }
    setSettings({ ...settings, apiKey: undefined, apiKeyId: undefined });
  }, [settings, setSettings]);

  return {
    logout,
  };
}

export function useIsLoggedIn() {
  const { settings, isLoading } = useAppSettings();

  return isLoading ? undefined : !!settings.apiKey;
}