UNPKG

ai-auth

Version:

Complete Auth-Agent SDK - Agent authentication for AI developers + OAuth client integration for website developers

77 lines 2.29 kB
"use strict"; /** * React hooks for Auth-Agent */ Object.defineProperty(exports, "__esModule", { value: true }); exports.useUser = useUser; exports.useAccessToken = useAccessToken; exports.useAuthenticatedFetch = useAuthenticatedFetch; exports.useAuthCallback = useAuthCallback; const react_1 = require("react"); const AuthContext_1 = require("./AuthContext"); /** * useUser hook * Get current user information with loading state */ function useUser() { const { user, isLoading, isAuthenticated, error } = (0, AuthContext_1.useAuth)(); return { user, isLoading, isAuthenticated, error, }; } /** * useAccessToken hook * Get current access token */ function useAccessToken() { const { getAccessToken } = (0, AuthContext_1.useAuth)(); return getAccessToken(); } /** * useAuthenticatedFetch hook * Wrapper around fetch that automatically adds Authorization header */ function useAuthenticatedFetch() { const { getAccessToken } = (0, AuthContext_1.useAuth)(); const authenticatedFetch = (0, react_1.useCallback)(async (url, options) => { const token = getAccessToken(); if (!token) { throw new Error('No access token available'); } const headers = new Headers(options?.headers); headers.set('Authorization', `Bearer ${token}`); return fetch(url, { ...options, headers, }); }, [getAccessToken]); return authenticatedFetch; } /** * useAuthCallback hook * Handle OAuth callback in a component */ function useAuthCallback() { const { client } = (0, AuthContext_1.useAuth)(); const [isHandling, setIsHandling] = (0, react_1.useState)(false); const [result, setResult] = (0, react_1.useState)(null); (0, react_1.useEffect)(() => { const handleCallback = async () => { if (window.location.search.includes('code=') && !isHandling) { setIsHandling(true); const callbackResult = await client.handleCallback(); setResult(callbackResult); setIsHandling(false); } }; handleCallback(); }, [client, isHandling]); return { isHandling, result, }; } //# sourceMappingURL=hooks.js.map