Skip to content

The open alternative to Salesforce, designed for AI.

1.9 KB
Raw Blame
import { useMutation } from '@apollo/client/react';
import { useCallback } from 'react';

import { useRedirect } from '@/domain-manager/hooks/useRedirect';
import { REACT_APP_SERVER_BASE_URL } from '~/config';
import { GenerateTransientTokenDocument } from '~/generated-metadata/graphql';

// Mints a transient token then redirects to the generic app OAuth endpoint.
// Mirrors `useTriggerApisOAuth` for Google/Microsoft, but the URL template is
// /auth/apps/authorize and works for any app-declared OAuth provider.
export const useTriggerAppOAuth = () => {
  const [generateTransientToken] = useMutation(GenerateTransientTokenDocument);
  const { redirect } = useRedirect();

  const triggerAppOAuth = useCallback(
    async ({
      applicationId,
      providerName,
      reconnectingConnectedAccountId,
      redirectLocation,
    }: {
      applicationId: string;
      providerName: string;
      // Set to update an existing connectedAccount row rather than creating
      // a new one (the "Reconnect" action on a failed credential).
      reconnectingConnectedAccountId?: string;
      redirectLocation?: string;
    }) => {
      const transient = await generateTransientToken();
      const token = transient.data?.generateTransientToken.transientToken.token;

      if (!token) {
        return;
      }

      const params = new URLSearchParams({
        applicationId,
        providerName,
        transientToken: token,
      });

      if (reconnectingConnectedAccountId) {
        params.set(
          'reconnectingConnectedAccountId',
          reconnectingConnectedAccountId,
        );
      }

      if (redirectLocation) {
        params.set('redirectLocation', redirectLocation);
      }

      redirect(
        `${REACT_APP_SERVER_BASE_URL}/auth/apps/authorize?${params.toString()}`,
      );
    },
    [generateTransientToken, redirect],
  );

  return { triggerAppOAuth };
};