-(function(s){typeof define=="function"&&define.amd?define(s):s()})(function(){"use strict";const s="/actions/users/session-info",c=async function(){return await fetch(s,{headers:{Accept:"application/json"}}).then(t=>t.json())};let a=null;const u=t=>{const e=new URL(t).pathname,o=window.location.pathname.split("/");o.pop();const d=o.join("/");return e.replace(d,"").replace(/^\//,"")},r=()=>{const t=document.head.querySelector("meta[csrf]");if(!t)return null;const n=t==null?void 0:t.getAttribute("name"),e=t==null?void 0:t.getAttribute("content");return{csrfTokenName:n??"CRAFT_CSRF_TOKEN",csrfTokenValue:e??""}},i=(t,n)=>{let e=document.head.querySelector("meta[csrf]");e?(e.setAttribute("name",t),e.setAttribute("content",n)):(e=document.createElement("meta"),e.setAttribute("csrf",""),e.setAttribute("name",t),e.setAttribute("content",n),document.head.appendChild(e))},l=async()=>{window.axios.defaults.headers={"Content-Type":"multipart/form-data"},window.axios.interceptors.request.use(async t=>{if(t.method==="post"||t.method==="put"){let n=r();n||(a=await c(),a.isGuest||(i(a.csrfTokenName,a.csrfTokenValue),n=r()));const e=n||a;if(!e)throw new Error("CSRF token not found");const o=u(t.url);t.url="",t.data instanceof FormData?(t.data.append(e.csrfTokenName,e.csrfTokenValue),t.data.append("action",o)):t.data={[e.csrfTokenName]:e.csrfTokenValue,action:o,...t.data}}return t}),window.axios.interceptors.response.use(async t=>(t.config.data.get("action")=="users/login"&&await c().then(n=>{i(n.csrfTokenName,n.csrfTokenValue)}),t))};(async()=>{let n=0;const e=setInterval(async()=>{if(window.axios){clearInterval(e),await l();return}n++,n>=40&&(clearInterval(e),console.warn("Inertia (Craft): Axios not found after 10 seconds. CSRF protection may not be active."))},250)})()});
0 commit comments