fix creation/auth process
This commit is contained in:
		
							
								
								
									
										301
									
								
								wco/apx/apx.js
									
									
									
									
									
								
							
							
						
						
									
										301
									
								
								wco/apx/apx.js
									
									
									
									
									
								
							| @@ -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); | ||||
|   }); | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user