aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mobile/lib/session.ts
blob: 8eb646cb13d09404c0f1a6d2c564104f4a1dbe95 (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
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(() => {
    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;
}