UNPKG

analytica-frontend-lib

Version:

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

148 lines (147 loc) 4.94 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/components/Auth/useUrlAuthentication.ts var useUrlAuthentication_exports = {}; __export(useUrlAuthentication_exports, { useUrlAuthentication: () => useUrlAuthentication }); module.exports = __toCommonJS(useUrlAuthentication_exports); var import_react = require("react"); var import_react_router_dom = require("react-router-dom"); var getAuthParams = (location, extractParams) => { const searchParams = new URLSearchParams(location.search); return extractParams ? extractParams(searchParams) : { sessionId: searchParams.get("sessionId"), token: searchParams.get("token"), refreshToken: searchParams.get("refreshToken") }; }; var hasValidAuthParams = (authParams) => { return !!(authParams?.sessionId && authParams?.token && authParams?.refreshToken); }; var hasValidProfileData = (data) => { return data !== null && typeof data === "object" && data !== void 0; }; var handleProfileSelection = (responseData, setSelectedProfile) => { if (!setSelectedProfile) return; if (!hasValidProfileData(responseData)) return; const profileId = responseData.profileId; const isValidProfileId = profileId !== null && profileId !== void 0; if (isValidProfileId) { setSelectedProfile({ id: profileId }); } }; var handleUserData = (responseData, setUser) => { if (!setUser) return; if (!hasValidProfileData(responseData)) return; const userId = responseData.userId; const userName = responseData.userName; const userEmail = responseData.userEmail; if (userId) { const userData = { id: userId }; if (userName) { userData.name = userName; } if (userEmail) { userData.email = userEmail; } setUser(userData); } }; function useUrlAuthentication(options) { const location = (0, import_react_router_dom.useLocation)(); const processedRef = (0, import_react.useRef)(false); (0, import_react.useEffect)(() => { const handleAuthentication = async () => { if (processedRef.current) { return; } const authParams = getAuthParams(location, options.extractParams); if (!hasValidAuthParams(authParams)) { return; } processedRef.current = true; try { options.setTokens({ token: authParams.token, refreshToken: authParams.refreshToken }); const maxRetries = options.maxRetries || 3; const retryDelay = options.retryDelay || 1e3; let retries = 0; let sessionData = null; while (retries < maxRetries && !sessionData) { try { const response = await options.api.get(options.endpoint, { headers: { Authorization: `Bearer ${authParams.token}` } }); sessionData = response.data.data; break; } catch (error) { retries++; console.warn( `Tentativa ${retries}/${maxRetries} falhou para ${options.endpoint}:`, error ); if (retries < maxRetries) { await new Promise( (resolve) => setTimeout(resolve, retryDelay * retries) ); } else { throw error; } } } options.setSessionInfo(sessionData); handleProfileSelection(sessionData, options.setSelectedProfile); handleUserData(sessionData, options.setUser); options.clearParamsFromURL?.(); } catch (error) { console.error("Erro ao obter informa\xE7\xF5es da sess\xE3o:", error); processedRef.current = false; options.onError?.(error); } }; handleAuthentication(); }, [ location.search, options.setSessionInfo, options.setSelectedProfile, options.setUser, options.setTokens, options.api, options.endpoint, options.extractParams, options.clearParamsFromURL, options.maxRetries, options.retryDelay, options.onError ]); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useUrlAuthentication }); //# sourceMappingURL=index.js.map