import { ListableResourceType } from '@commercelayer/sdk'; import { ReactNode } from 'react'; import { TokenProviderAllowedAppSlug, TokenProviderAuthSettings, TokenProviderAuthUser, TokenProviderClAppSlug, TokenProviderExtras, TokenProviderRole, TokenProviderRoleActions, TokenProviderTokenApplicationKind } from './types'; export interface TokenProviderValue { settings: TokenProviderAuthSettings; user: TokenProviderAuthUser | null; canUser: (action: TokenProviderRoleActions, resource: ListableResourceType) => boolean; canAccess: (appSlug: TokenProviderClAppSlug) => boolean; emitInvalidAuth: (reason: string) => void; role: TokenProviderRole | null; } export interface TokenProviderProps { /** * The token kind (will be validated). * For example: `integration`, `sales_channel` or `webapp` but also `imports` or `orders` in case of dashboard-generated token. */ kind: TokenProviderTokenApplicationKind; /** * The slug of the current app. It could match one of the allowed apps or a custom string. * It is used as the app identifier (e.g. storage key). */ appSlug: TokenProviderAllowedAppSlug; /** * Set this to `true` to skip domain slug validation in dev mode. */ devMode: boolean; /** * The entire app content. */ children: ((props: TokenProviderValue) => ReactNode) | ReactNode; /** * If present, only an access token with the same organization slug will be considered valid. * It will be also used to generate the local storage key when the token is persisted (unless a custom `storage` methods are provided). * When empty, the app will use the organization slug decoded from the token * and the token will be persisted using a default key ('commercelayer'). */ organizationSlug?: string; /** * Optional. Override the internal logic to persist the access token and extra (save and retrieve). */ storage?: { /** Custom method to retrieve the access token. It must return a valid and not expired access token */ getAccessToken: () => string | null; /** Custom method to save the access token in your preferred storage */ saveAccessToken: (token: string) => void; /** Custom method to retrieve the encoded `extra` data, if provided */ getEncodedExtra?: () => string | null; /** Custom method to save the encoded `extra` data, if provided, in your preferred storage */ saveEncodedExtra?: (encodedExtra: string) => void; }; /** * The callback invoked when token is not valid. * Can be used to manually handle the re-authentication flow when `reauthenticateOnInvalidAuth` is false. */ onInvalidAuth?: (info: { dashboardUrl: string; reason: string; }) => void; /** * The UI element to be used as loader (e.g. skeleton or spinner icon). * This element is ignored when app is running within the dashboard, since it will use the standard PageSkeleton ad not-overridable ui loader. */ loadingElement?: ReactNode; /** * The element to display in case of invalid token. */ errorElement?: ReactNode; /** * Optional. In case you already have an access token, this will skip the retrieval of token from the URL or `localStorage`. * When `undefined` (default scenario), the token is expected to be retrieved from the `?accessToken=xxxx` query string or localStorage (in this order). */ accessToken?: string; /** * Optional. Set to `true` to enable the app to be used within the dashboard. * In this way the UI can be adapted. */ isInDashboard?: boolean; /** * Optional. Define a callback to be invoked when the app is closed. * When this is defined, it means the app is running as micro-frontend (eg: initialized from the dashboard, that needs to handle the go back). * This methods will also be returned within the context as part of `TokenProviderValue` object. */ onAppClose?: () => void; /** * Optional. Extra data to pass from the dashboard or when the app is initialized. * They will be received on app init and make available in the TokenProvider > settings context. */ extras?: TokenProviderExtras; } export declare const AuthContext: import('react').Context; export declare const useTokenProvider: () => TokenProviderValue; export declare const TokenProvider: React.FC;