frontend client for gemstone. decentralised workplace app
1import { useOAuthAgent, useOAuthSession } from "@/providers/OAuthProvider";
2import type { Agent } from "@atproto/api";
3import type { OAuthSession } from "@atproto/oauth-client";
4import { useQuery } from "@tanstack/react-query";
5
6export const useIsaJwt = () => {
7 const agent = useOAuthAgent();
8 const session = useOAuthSession();
9 const { data, error, isPending } = useQuery({
10 queryKey: ["isa-jwt"],
11 queryFn: async () => {
12 return await requestIsaJwt({ agent, session });
13 },
14 });
15 if (error)
16 throw new Error(
17 `something went wrong when trying to request the inter-service JWT ${JSON.stringify(error)}`,
18 );
19 return { data, isPending };
20};
21
22const requestIsaJwt = async ({
23 agent,
24 session,
25}: {
26 agent: Agent | undefined;
27 session: OAuthSession | undefined;
28}) => {
29 if (!agent || !session) return;
30 const { data, success } = await agent.com.atproto.server.getServiceAuth({
31 aud: session.did,
32 });
33 if (!success)
34 throw new Error(
35 `something went wrong calling com.atproto.server.getServiceAuth with did ${session.did}`,
36 );
37 return { token: data.token };
38};