fix creation/auth process

This commit is contained in:
2025-09-01 07:20:17 +02:00
parent d218c3b1d0
commit 2ac5e9ce07
6 changed files with 214 additions and 215 deletions

View File

@@ -227,9 +227,9 @@ apx.listendatawco = (newpropertie) => {
elements.forEach((e) => actionprop(newv, e));
},
});
if (_val !== undefined) {
apx.data.wco[p] = _val;
}
if (_val !== undefined) {
apx.data.wco[p] = _val;
}
}
});
};
@@ -336,64 +336,7 @@ apx.wcoobserver = () => {
e.setAttribute("wco-name", wconame);
});
};
// State management
apx.save = () => {
localStorage.setItem(apx.data.headers.xapp, JSON.stringify(apx.data));
};
apx.update = async () => {
if (!apxtri) {
console.log(
'Please add to the html page header, this line const apxtri = { headers: { xtrkversion: 1, xtribe: "smatchit", xapp: "pwa", xlang: "fr", xalias: "anonymous", xhash: "anonymous", xdays: 0} ,pagename:"apxid"} '
);
return;
}
//if (apxtri.forcereload){localStorage.setItem("forcereload",true)};
if (document.querySelector("html").getAttribute("lang")) {
apxtri.headers.xlang = document.querySelector("html").getAttribute("lang");
}
//alert(localStorage.getItem(apxtri.headers.xapp))
if (localStorage.getItem(apxtri.headers.xapp)) {
apx.data = JSON.parse(localStorage.getItem(apxtri.headers.xapp));
//update with current pagename and eventualy pageauth
apx.data.pagename = apxtri.pagename;
if (apxtri.pageauth) apx.data.pageauth = apxtri.pageauth;
// check localstorage in line with current webpage
if (
apx.data.headers.xtribe != apxtri.headers.xtribe ||
apx.data.headers.xlang != apxtri.headers.xlang ||
apx.data.headers.xtrkversion != apxtri.headers.xtrkversion
) {
// if an app change of tribe
localStorage.removeItem(apxtri.headers.xapp);
delete apx.data;
}
}
if (!apx.data) {
console.log("init or reinit apx.data");
apx.data = apxtri;
}
apx.pagecontext = { search: {}, hash: {} };
if (window.location.hash != "") {
window.location.hash
.slice(1)
.split("&")
.forEach((kv) => {
const keyval = kv.split("=");
apx.pagecontext.hash[keyval[0]] = keyval[1];
});
}
if (window.location.search != "") {
window.location.search
.slice(1)
.split("&")
.forEach((kv) => {
const keyval = kv.split("=");
apx.pagecontext.hash[keyval[0]] = keyval[1];
});
}
console.log("apx.pagecontext:", apx.pagecontext);
apx.managecontext = () => {
// Set authenticate parameter if in pagecontext and redirect to the requested url
console.log(
apx.pagecontext.hash.xdays,
@@ -413,17 +356,12 @@ apx.update = async () => {
) {
//Means this page is called from an external auth app
let headervalid = true;
const headerkey = [
"xalias",
"xhash",
"xdays",
"xprofils",
"xtribe",
"xlang",
];
headerkey.forEach((h) => {
if (apx.pagecontext.hash[h]) {
apx.data.headers[h] = (h==="xprofils")? apx.pagecontext.hash[h].split(","):apx.pagecontext.hash[h];
apx.data.headers[h] =
h === "xprofils"
? apx.pagecontext.hash[h].split(",")
: apx.pagecontext.hash[h];
} else {
headervalid = false;
}
@@ -438,6 +376,85 @@ apx.update = async () => {
console.log("Your try to access a page failled with ", apx.pagecontext);
}
}
};
// State management
apx.save = () => {
localStorage.setItem(apx.data.headers.xapp, JSON.stringify(apx.data));
};
apx.update = async () => {
if (!apxtri || !apxtri.headers || !apxtri.headers.xapp) {
console.log(
'Please add to the html page header, this line const apxtri = { headers: { xtrkversion: 1, xtribe: "smatchit", xapp: "pwa", xlang: "fr", xalias: "anonymous", xhash: "anonymous", xdays: 0} ,pagename:"apxid"} '
);
return;
}
const headerkey = ["xalias", "xhash", "xdays", "xprofils", "xtribe", "xlang"];
if (document.querySelector("html").getAttribute("lang")) {
apxtri.headers.xlang = document.querySelector("html").getAttribute("lang");
}
apxtri.headers.xlang = apxtri.headers?.xlang || "en";
//Get if exist in localstorage a key apxtri.headers.xapp
if (apxtri.headers.xapp && localStorage.getItem(apxtri.headers.xapp)) {
apx.data = JSON.parse(localStorage.getItem(apxtri.headers.xapp));
//update with current pagename and eventualy pageauth
apx.data.pagename = apxtri.pagename;
if (apxtri.pageauth) apx.data.pageauth = apxtri.pageauth;
const isauth = await axios.get('/api/apxtri/pagans/isauth', { headers: apx.data.headers, withCredentials:true,validateStatus: (status) => true});
if (
(isauth.status!==200 && apx.data.headers.alias!=="anonymous") ||
apx.data.headers.xtribe != apxtri.headers.xtribe ||
apx.data.headers.xlang != apxtri.headers.xlang ||
apx.data.headers.xtrkversion != apxtri.headers.xtrkversion
) {
// if no more authenticated or change of tribe...
localStorage.removeItem(apxtri.headers.xapp);
delete apx.data;
}
}
//reinit if apx.data does not exist
apx.data = apx?.data || apxtri;
// get context from url search ?q=x&s=z or hash #q=x&s=z
// search comme from a reloading page instead of hash is just a passive link
apx.pagecontext = { search: {}, hash: {} };
["hash", "search"].forEach((type) => {
const paramString = window.location[type];
if (paramString) {
paramString
.slice(1)
.split("&")
.forEach((kv) => {
const [key, value] = kv.split("=");
apx.pagecontext.hash[key] = value;
});
}
});
console.log("apx.pagecontext:", apx.pagecontext);
// check is authenticated
if (
apx.data.headers.xalias === "anonymous" ||
(apx.pagecontext.hash.xdays &&
dayjs(apx.pagecontext.hash.xdays).diff(dayjs(), "hours") < 25)
) {
// reset authentification data
apx.data.headers.xalias = "anonymous";
apx.data.headers.xdays = 0;
apx.data.headers.xprofils = ["anonymous"];
delete apx.data.headers.xhash;
}
//apx.managecontext(); (todo when all work target is to use context to redirect if key word used in contexte url, ....)
if (
apx.data.headers.xalias === "anonymous" &&
(apx.data.auth || !apx.data.allowedprofils.includes("anonymous"))
) {
// Means user is not authenticat and has a rememberMe previously selected (auth) OR this page is not accessible to an anonymous.
// Redirect to apxid page if auth is ok then redirect to this page
// notauth then expected to have alias privatekey
//document.location.href = `/apxtriadmin/${apx.data.pageauth}_${apx.data.headers.xlang}.html#url=${apx.data.
document.location.href = `/apxtriadmin/apxid_${apx.data.headers.xlang}.html#url=${apx.data.pagename}_${apx.data.headers.xlang}.html`;
}
// check accessright to the page
if (
apx.data.allowedprofils &&
!apx.data.allowedprofils.includes("anonymous") &&
@@ -449,13 +466,9 @@ apx.update = async () => {
console.log("profils authorized:", profilintersect);
if (profilintersect.length == 0) {
alert(apx.data.ref.Middlewares.notallowtoaccess);
document.location.href = `/apxtriadmin/apxid_${apx.data.headers.xlang}.html`
return false;
}
if (dayjs().valueOf() - apx.data.headers.xdays > 86400000) {
// need to refresh authentification if possible by opening the pageauth with url context
// the pageauth redirect to this current page after authentification, if not then wait credential
document.location.href = `/${apx.data.pageauth}_${apx.data.headers.xlang}.html#url=${apx.data.pagename}_${apx.data.headers.xlang}.html`;
}
}
console.log("authorized to access");
/* à voir si utile redirect to authentification page pageauth with a redirection if authentify to the pagename (check if /src/ then add it)
@@ -465,6 +478,11 @@ apx.update = async () => {
apxtri.pagename
}_${apxtri.headers.xlang}.html`;
*/
/// axios setting //
axios.defaults.withCredentials = true; // force to send cookie http only for authentification
axios.defaults.headers.common=apx.data.headers;
////////////////////
////////////////////////////////////////////
apx.data.version = 0; //this force an update to be removed in production
///////////////////////////////////////////
@@ -473,8 +491,7 @@ apx.update = async () => {
let initset = {};
try {
initset = await axios.get(initdb, {
headers: apx.data.headers,
timeout: 2000,
timeout: 2000
});
} catch (err) {
console.log(err);
@@ -491,21 +508,11 @@ apx.update = async () => {
setTimeout(apx.update, 30000);
}
if (initset.data.msg == "data_model_update") {
// mise à jour local
/*if (initset.data.data.wco) {
console.log("WARNING!!, local apxtri.wco was erase by updatelocaldb.wco");
}*/
Object.keys(initset.data.data).forEach((k) => {
if (k !== "headers") {
apx.data[k] = initset.data.data[k];
}
});
/* if (apx.data.confpage.wco && !apx.data.wco){
console.log("update apx.data.wco with localdb cause does not exist")
apx.data.wco=apx.data.confpage.wco;
}
*/
console.log("local update done");
apx.save();
}
@@ -521,84 +528,84 @@ apx.ready(apx.update); //2nd param optional=> true mean does not wait same if ap
apx.indexedDB = apx.indexedDB || {};
apx.indexedDB.set = async (db, storeName, value) => {
return new Promise((resolve, reject) => {
const request = indexedDB.open(db, 1);
return new Promise((resolve, reject) => {
const request = indexedDB.open(db, 1);
request.onupgradeneeded = (event) => {
const db = event.target.result;
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains("threads")) {
db.createObjectStore("threads", { keyPath: "uuid" });
};
if (!db.objectStoreNames.contains("threads")) {
db.createObjectStore("threads", { keyPath: "uuid" });
}
if (!db.objectStoreNames.contains("messages")) {
db.createObjectStore("messages", { keyPath: "privatriid" });
};
};
if (!db.objectStoreNames.contains("messages")) {
db.createObjectStore("messages", { keyPath: "privatriid" });
}
};
request.onsuccess = (event) => {
const db = event.target.result;
request.onsuccess = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(storeName)) {
return resolve();
};
if (!db.objectStoreNames.contains(storeName)) {
return resolve();
}
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
const putRequest = store.put(value);
putRequest.onsuccess = () => resolve();
putRequest.onerror = (error) => reject(error);
};
const putRequest = store.put(value);
putRequest.onsuccess = () => resolve();
putRequest.onerror = (error) => reject(error);
};
request.onerror = (error) => reject(error);
});
request.onerror = (error) => reject(error);
});
};
apx.indexedDB.get = async (db, storeName, key) => {
return new Promise((resolve, reject) => {
const request = indexedDB.open(db, 1);
apx.indexedDB.get = async (db, storeName, key) => {
return new Promise((resolve, reject) => {
const request = indexedDB.open(db, 1);
request.onsuccess = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(storeName)) {
return resolve(null);
}
const transaction = db.transaction(storeName, "readonly");
const store = transaction.objectStore(storeName);
const getRequest = store.get(key);
getRequest.onsuccess = () => {
resolve(getRequest.result || null);
};
request.onsuccess = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(storeName)) {
return resolve(null);
}
const transaction = db.transaction(storeName, "readonly");
const store = transaction.objectStore(storeName);
getRequest.onerror = () => resolve(null);
};
const getRequest = store.get(key);
request.onerror = (error) => reject(error);
});
getRequest.onsuccess = () => {
resolve(getRequest.result || null);
};
getRequest.onerror = () => resolve(null);
};
request.onerror = (error) => reject(error);
});
};
apx.indexedDB.del = async (db, storeName, key) => {
return new Promise((resolve, reject) => {
const request = indexedDB.open(db, 1);
return new Promise((resolve, reject) => {
const request = indexedDB.open(db, 1);
request.onsuccess = (event) => {
const db = event.target.result;
request.onsuccess = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(storeName)) {
return resolve();
};
if (!db.objectStoreNames.contains(storeName)) {
return resolve();
}
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
const transaction = db.transaction(storeName, "readwrite");
const store = transaction.objectStore(storeName);
const deleteRequest = store.delete(key);
deleteRequest.onsuccess = () => resolve();
deleteRequest.onerror = (error) => reject(error);
};
const deleteRequest = store.delete(key);
deleteRequest.onsuccess = () => resolve();
deleteRequest.onerror = (error) => reject(error);
};
request.onerror = (error) => reject(error);
});
};
request.onerror = (error) => reject(error);
});
};

View File

@@ -11,6 +11,8 @@ apx.apxauth.loadwco = async (id, ctx) => {
ctx
)}`
);
// Check that in localdb tpl exist if not means it is not authenticated
if (!apx.data.tpl[`apxauthscreen${ctx.link}`]) ctx.link="signin";
const tpldataname = `${apx.data.pagename}_${id}_apxauth`;
const apxauthid = document.getElementById(id);
const data = apx.apxauth.getdata(id, ctx);
@@ -31,7 +33,7 @@ apx.apxauth.getdata = (id, ctx) => {
data.xalias = apx.data.headers.xalias;
data.xtribe = apx.data.headers.xtribe;
data.emailssuport = apx.data.appdata.emailsupport
data.emailssuport = apx.data?.appdata?.emailsupport
? apx.data.appdata.emailsupport
: "";
switch (ctx.link) {
@@ -44,7 +46,7 @@ apx.apxauth.getdata = (id, ctx) => {
});
data.noprofils = data.profils.length == 0;
data.member = apx.data.headers.xprofils.includes("persons");
data.websites = apx.data.appdata.websites;
data.websites = apx.data?.appdata?.websites || [];
data.optionlinks=apx.data.tpldata[tpldataname].optionlinks
data.town=apx.data.town
data.nation=apx.data.nation
@@ -52,10 +54,13 @@ apx.apxauth.getdata = (id, ctx) => {
apx.data.tpldata[tpldataname].optionlinksmajor.forEach(o=>data.optionlinks.push(o))
apx.save()
}
if (!apx.data.itms) apx.data.itms={}
if (!apx.data.wco) apx.data.wco={}
// get towns list
axios
.get(`/api/apxtri/odmdb/idx/apxtri/towns/towns`, {
headers: apx.data.headers,
headers: apx.data.headers, withCredentials:true
})
.then((rep) => {
console.log(rep)
@@ -72,7 +77,7 @@ apx.apxauth.getdata = (id, ctx) => {
});
axios
.get(`/api/apxtri/odmdb/idx/apxtri/pagans/lst_alias`, {
headers: apx.data.headers,
headers: apx.data.headers, withCredentials:true
})
.then((rep) => {
if (rep.status==200){
@@ -139,7 +144,7 @@ apx.apxauth.redirecturlwithauth = (
apx.apxauth.logout = () => {
axios
.get(`/api/apxtri/pagans/logout`, {
headers: apx.data.headers,
headers: apx.data.headers, withCredentials:true
})
.then((rep) => {
console.log("logout", rep);
@@ -250,7 +255,7 @@ apx.apxauth.authentifyme = async (
console.log(`get /api/apxtri/pagans/alias/${alias}`);
axios
.get(`/api/apxtri/pagans/alias/${alias}`, {
headers: apx.data.headers,
headers: apx.data.headers, withCredentials:true
})
.then(async (rep) => {
//console.log(rep.data);
@@ -358,7 +363,7 @@ apx.apxauth.recoverykey = (id, aliasoremail) => {
document.querySelector(`#${id} .msginfo`).innerHTML = "";
axios
.post(`/api/apxtri/pagans/keyrecovery`, recodata, {
headers: apx.data.headers,
headers: apx.data.headers
})
.then((rep) => {
rep.data.data.search = aliasoremail;
@@ -678,15 +683,14 @@ apx.apxauth.registerIdentity = async (id, trustedtribe) => {
data.trustedtribe = trustedtribe;
console.log(data)
axios
.post(`/api/apxtri/pagans`, data, { headers: apx.data.headers })
.post(`/api/apxtri/pagans`, data, { headers: apx.data.headers, withCredentials:true})
.then((reppagan) => {
//console.log(reppagan.data);
apx.notification(`#${id} .msginfo`, reppagan.data);
authid.querySelector(`.btncreateidentity`).classList.add("hidden");
authid.querySelector(`.signupbtnreload`).classList.remove("hidden");
//remove tmp cause create phc change to keep tplauth in memory and avoid asking again the pasword
//delete apx.data.tmpauth;
//apx.save();
apx.data.headers.xprofils=reppagan.data.profils;
apx.data.save();
})
.catch((err) => {
console.log("error:", err);
@@ -713,13 +717,13 @@ apx.apxauth.jointribe = (id) => {
};
axios
.put(`/api/apxtri/pagans/person/${apx.data.headers.xtribe}`, data, {
headers: apx.data.headers,
headers: apx.data.headers, withCredentials:true
})
.then((rep) => {
apx.notification(`#${id} .msginfo`, rep.data);
axios
.get(`/api/apxtri/pagans/logout`, {
headers: apx.data.headers,
headers: apx.data.headers, withCredentials:true
})
.then((rep) => {
console.log("logout", rep);

View File

@@ -5,7 +5,9 @@ apx.simplemobnav.loadwco = (id, ctx) => {
const tpldataname = `${apx.data.pagename}_${id}_simplemobnav`;
const simplemobnavid = document.getElementById(id)
console.log("load simplemobnav with tpldataname:", tpldataname, " id:", id, " ctx:", ctx);
// check if authenticate if yes then show myworld instead of signin
//console.log("ggggggggggggg",apx.data.headers.xalias)
//if (apx.data.headers.xalias!="anonymous") ctx.link="myworld";
let initmenu;
if (simplemobnavid.innerHTML.trim() === "") {
// Get 1st menu matching the first profil in profilmenu