'use client'; import { type FetcherOptions, getIntlayerAPI, type IntlayerAPI, } from '@intlayer/api'; import { getAiAPI } from '@intlayer/api/ai'; import { getAnalyticsAPI } from '@intlayer/api/analytics'; import { getAssetAPI } from '@intlayer/api/asset'; import { getAuditAPI } from '@intlayer/api/audit'; import { getBitbucketAPI } from '@intlayer/api/bitbucket'; import { getCodebergAPI } from '@intlayer/api/codeberg'; import { getDictionaryAPI } from '@intlayer/api/dictionary'; import { getEditorAPI } from '@intlayer/api/editor'; import { getEnvironmentAPI } from '@intlayer/api/environment'; import { getGiteeAPI } from '@intlayer/api/gitee'; import { getGithubAPI } from '@intlayer/api/github'; import { getGitlabAPI } from '@intlayer/api/gitlab'; import { getNewsletterAPI } from '@intlayer/api/newsletter'; import { getOAuthAPI } from '@intlayer/api/oAuth'; import { getOrganizationAPI } from '@intlayer/api/organization'; import { getProjectAPI } from '@intlayer/api/project'; import { getReviewerAPI } from '@intlayer/api/reviewer'; import { getSearchAPI } from '@intlayer/api/search'; import { getShowcaseProjectAPI } from '@intlayer/api/showcaseProject'; import { getStripeAPI } from '@intlayer/api/stripe'; import { getTagAPI } from '@intlayer/api/tag'; import { getTranslateAPI } from '@intlayer/api/translate'; import { getUserAPI } from '@intlayer/api/user'; import { HEADER_NAME } from '@intlayer/config/defaultValues'; import { useConfiguration } from '@intlayer/editor-react'; import type { IntlayerConfig } from '@intlayer/types/config'; import { useLocale } from 'react-intlayer'; import { type AuthAPI, getAuthAPI } from '../libs/auth'; import { useBearerAuth } from './useAuth/BearerAuthProvider'; export type UseIntlayerAuthProps = { options?: FetcherOptions; intlayerConfiguration?: IntlayerConfig; /** * A short-lived token identifying the **signed-in user**, sent as a bearer. * * Only needed when the session cookie cannot be used — the backend allows * credentialed requests for first-party origins only, so an app served from * another origin has to carry the user's identity explicitly. * * The project's `clientId` / `clientSecret` are deliberately not an option * here: they authenticate as the *project*, grant write access to all of its * content, and must never reach a browser. */ sessionToken?: string; }; export const useIntlayerOAuthOptions = (props?: UseIntlayerAuthProps) => { const configuration = useConfiguration(); const { locale } = useLocale(); const bearerAuth = useBearerAuth(); const sessionToken = props?.sessionToken ?? bearerAuth?.accessToken; // On a first-party origin the session cookie authenticates every call (the // fetcher sends `credentials: 'include'`), so no header is needed. Elsewhere // the caller supplies a user session token. // The locale travels as a header: the locale cookie is host-only, so it never // reaches a backend served from another host (e.g. `back.intlayer.org`). const options: FetcherOptions = { headers: { [HEADER_NAME]: locale, ...(sessionToken && { Authorization: `Bearer ${sessionToken}`, }), }, ...(props?.options ?? {}), }; const rawConfig = props?.intlayerConfiguration ?? configuration; // Only use the config if it's fully populated; an empty object (e.g. while // the session is still loading) has no `editor` key and would crash API // getters that do `intlayerConfig.editor.backendURL`. const resolvedConfig = rawConfig?.editor ? rawConfig : undefined; return { options, resolvedConfig }; }; export const useIntlayerOAuth = (props?: UseIntlayerAuthProps): IntlayerAPI => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getIntlayerAPI(options, resolvedConfig); }; export const useIntlayerAuth = (props?: UseIntlayerAuthProps): AuthAPI => { const configuration = useConfiguration(); return getAuthAPI(props?.intlayerConfiguration ?? configuration); }; export const useOrganizationAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getOrganizationAPI(options, resolvedConfig); }; export const useProjectAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getProjectAPI(options, resolvedConfig); }; export const useUserAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getUserAPI(options, resolvedConfig); }; export const useOAuthAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getOAuthAPI(options, resolvedConfig); }; export const useDictionaryAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getDictionaryAPI(options, resolvedConfig); }; export const useStripeAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getStripeAPI(options, resolvedConfig); }; export const useAiAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getAiAPI(options, resolvedConfig); }; export const useAuditAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getAuditAPI(options, resolvedConfig); }; export const useAnalyticsAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getAnalyticsAPI(options, resolvedConfig); }; export const useTagAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getTagAPI(options, resolvedConfig); }; export const useSearchAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getSearchAPI(options, resolvedConfig); }; export const useEditorAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getEditorAPI(options, resolvedConfig); }; export const useNewsletterAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getNewsletterAPI(options, resolvedConfig); }; export const useGithubAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getGithubAPI(options, resolvedConfig); }; export const useGitlabAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getGitlabAPI(options, resolvedConfig); }; export const useBitbucketAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getBitbucketAPI(options, resolvedConfig); }; export const useCodebergAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getCodebergAPI(options, resolvedConfig); }; export const useGiteeAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getGiteeAPI(options, resolvedConfig); }; export const useShowcaseProjectAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getShowcaseProjectAPI(options, resolvedConfig); }; export const useTranslateAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getTranslateAPI(options, resolvedConfig); }; export const useReviewerAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getReviewerAPI(options, resolvedConfig); }; export const useEnvironmentAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getEnvironmentAPI(options, resolvedConfig); }; export const useAssetAPI = ( props?: UseIntlayerAuthProps ): ReturnType => { const { options, resolvedConfig } = useIntlayerOAuthOptions(props); return getAssetAPI(options, resolvedConfig); };