analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
126 lines (123 loc) • 4.82 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/Auth/useUrlAuthentication.ts
var _react = require('react');
var _reactrouterdom = 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 !!(_optionalChain([authParams, 'optionalAccess', _ => _.sessionId]) && _optionalChain([authParams, 'optionalAccess', _2 => _2.token]) && _optionalChain([authParams, 'optionalAccess', _3 => _3.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 = _reactrouterdom.useLocation.call(void 0, );
const processedRef = _react.useRef.call(void 0, false);
_react.useEffect.call(void 0, () => {
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);
if (options.onAuthHydrated) await options.onAuthHydrated();
_optionalChain([options, 'access', _4 => _4.clearParamsFromURL, 'optionalCall', _5 => _5()]);
} catch (error) {
console.error("Erro ao obter informa\xE7\xF5es da sess\xE3o:", error);
processedRef.current = false;
_optionalChain([options, 'access', _6 => _6.onError, 'optionalCall', _7 => _7(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,
options.onAuthHydrated
]);
}
exports.useUrlAuthentication = useUrlAuthentication;
//# sourceMappingURL=chunk-B4EFRRXH.js.map