UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

1 lines 3.09 kB
{"version":3,"sources":["../../../src/components/Auth/zustandAuthAdapter.ts"],"sourcesContent":["/**\n * Generic adapter for integrating AuthProvider with Zustand stores\n * Users should import this file and pass their store instance\n *\n * @template S - Zustand store type that contains auth-related state\n * @param {object} useAuthStore - Zustand store hook with getState method\n * @param {() => S} useAuthStore.getState - Function to get current store state\n * @returns {object} Adapter object with auth functions\n *\n * @example\n * ```typescript\n * // Define your Zustand store type\n * interface AuthStore {\n * sessionInfo?: SessionInfo;\n * tokens?: AuthTokens;\n * user?: AuthUser;\n * signOut: () => void;\n * }\n *\n * // Create the adapter\n * const authAdapter = createZustandAuthAdapter(useAuthStore);\n *\n * // Use with AuthProvider\n * <AuthProvider\n * checkAuthFn={authAdapter.checkAuth}\n * signOutFn={authAdapter.signOut}\n * getUserFn={authAdapter.getUser}\n * getSessionFn={authAdapter.getSessionInfo}\n * getTokensFn={authAdapter.getTokens}\n * >\n * <App />\n * </AuthProvider>\n * ```\n */\nexport function createZustandAuthAdapter<\n S extends {\n sessionInfo?: unknown;\n tokens?: unknown;\n user?: unknown;\n signOut?: () => void;\n },\n>(useAuthStore: { getState: () => S }) {\n return {\n /**\n * Check if the user is authenticated based on sessionInfo and tokens\n *\n * @returns {Promise<boolean>} Promise that resolves to authentication status\n */\n checkAuth: async (): Promise<boolean> => {\n const { sessionInfo, tokens } = useAuthStore.getState();\n return Boolean(sessionInfo && tokens);\n },\n /**\n * Get the current user from the store\n *\n * @returns {unknown} Current user data from the store\n */\n getUser: () => useAuthStore.getState().user,\n /**\n * Get the current session information from the store\n *\n * @returns {unknown} Current session info from the store\n */\n getSessionInfo: () => useAuthStore.getState().sessionInfo,\n /**\n * Get the current authentication tokens from the store\n *\n * @returns {unknown} Current tokens from the store\n */\n getTokens: () => useAuthStore.getState().tokens,\n /**\n * Sign out the user by calling the store's signOut function if available\n *\n * @returns {void}\n */\n signOut: () => {\n const signOutFn = useAuthStore.getState().signOut;\n if (typeof signOutFn === 'function') signOutFn();\n },\n };\n}\n"],"mappings":";AAkCO,SAAS,yBAOd,cAAqC;AACrC,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAML,WAAW,YAA8B;AACvC,YAAM,EAAE,aAAa,OAAO,IAAI,aAAa,SAAS;AACtD,aAAO,QAAQ,eAAe,MAAM;AAAA,IACtC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,SAAS,MAAM,aAAa,SAAS,EAAE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMvC,gBAAgB,MAAM,aAAa,SAAS,EAAE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAM9C,WAAW,MAAM,aAAa,SAAS,EAAE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAMzC,SAAS,MAAM;AACb,YAAM,YAAY,aAAa,SAAS,EAAE;AAC1C,UAAI,OAAO,cAAc,WAAY,WAAU;AAAA,IACjD;AAAA,EACF;AACF;","names":[]}