analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
94 lines (93 loc) • 3.36 kB
JavaScript
;
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
});
}
};
function useUrlAuthentication(options) {
const location = (0, import_react_router_dom.useLocation)();
(0, import_react.useEffect)(() => {
const handleAuthentication = async () => {
const authParams = getAuthParams(location, options.extractParams);
if (!hasValidAuthParams(authParams)) {
return;
}
try {
options.setTokens({
token: authParams.token,
refreshToken: authParams.refreshToken
});
const response = await options.api.get(options.endpoint, {
headers: {
Authorization: `Bearer ${authParams.token}`
}
});
options.setSessionInfo(response.data.data);
handleProfileSelection(response.data.data, options.setSelectedProfile);
options.clearParamsFromURL?.();
} catch (error) {
console.error("Erro ao obter informa\xE7\xF5es da sess\xE3o:", error);
}
};
handleAuthentication();
}, [
location.search,
options.setSessionInfo,
options.setSelectedProfile,
options.setTokens,
options.api,
options.endpoint,
options.extractParams,
options.clearParamsFromURL
]);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useUrlAuthentication
});
//# sourceMappingURL=index.js.map