analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
59 lines (57 loc) • 2.49 kB
TypeScript
/**
* Options interface for the useUrlAuthentication hook
*
* @template Tokens - Type for authentication tokens
* @template Session - Type for session information
* @template Profile - Type for profile information
*
* @interface UseUrlAuthOptions
* @property {(tokens: Tokens) => void} setTokens - Function to set authentication tokens
* @property {(session: Session) => void} setSessionInfo - Function to set session information
* @property {(profile: Profile) => void} [setSelectedProfile] - Optional function to set selected profile
* @property {object} api - API instance with get method
* @property {(endpoint: string, config: unknown) => Promise<unknown>} api.get - API get method
* @property {string} endpoint - API endpoint to fetch session data
* @property {(searchParams: URLSearchParams) => object} [extractParams] - Custom parameter extraction function
* @property {() => void} [clearParamsFromURL] - Function to clear URL parameters after processing
*/
interface UseUrlAuthOptions<Tokens = unknown, Session = unknown, Profile = unknown> {
setTokens: (tokens: Tokens) => void;
setSessionInfo: (session: Session) => void;
setSelectedProfile?: (profile: Profile) => void;
api: {
get: (endpoint: string, config: unknown) => Promise<unknown>;
};
endpoint: string;
extractParams?: (searchParams: URLSearchParams) => {
sessionId: string;
token: string;
refreshToken: string;
};
clearParamsFromURL?: () => void;
}
/**
* Hook for handling URL-based authentication
* Extracts authentication parameters from URL and processes them
*
* @template Tokens - Type for authentication tokens
* @template Session - Type for session information
* @template Profile - Type for profile information
*
* @param {UseUrlAuthOptions<Tokens, Session, Profile>} options - Configuration options
* @returns {void}
*
* @example
* ```typescript
* useUrlAuthentication({
* setTokens: (tokens) => authStore.setTokens(tokens),
* setSessionInfo: (session) => authStore.setSessionInfo(session),
* setSelectedProfile: (profile) => authStore.setProfile(profile),
* api: apiInstance,
* endpoint: '/auth/session',
* clearParamsFromURL: () => navigate('/', { replace: true })
* });
* ```
*/
declare function useUrlAuthentication<Tokens = unknown, Session = unknown, Profile = unknown>(options: UseUrlAuthOptions<Tokens, Session, Profile>): void;
export { type UseUrlAuthOptions, useUrlAuthentication };