Skip to content

Commit aa4cf58

Browse files
committed
push build
1 parent 5f1532e commit aa4cf58

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/web/assets/axioshook/.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ pnpm-debug.log*
88
lerna-debug.log*
99

1010
node_modules
11-
dist
12-
dist-ssr
1311
*.local
1412

1513
# Editor directories and files
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const u = "/actions/users/session-info", s = async function() {
2+
return await fetch(u, {
3+
headers: { Accept: "application/json" }
4+
}).then((t) => t.json());
5+
};
6+
let o = null;
7+
const l = (t) => {
8+
const e = new URL(t).pathname, a = window.location.pathname.split("/");
9+
a.pop();
10+
const i = a.join("/");
11+
return e.replace(i, "").replace(/^\//, "");
12+
}, r = () => {
13+
const t = document.head.querySelector("meta[csrf]");
14+
if (!t)
15+
return null;
16+
const n = t == null ? void 0 : t.getAttribute("name"), e = t == null ? void 0 : t.getAttribute("content");
17+
return {
18+
csrfTokenName: n ?? "CRAFT_CSRF_TOKEN",
19+
csrfTokenValue: e ?? ""
20+
};
21+
}, c = (t, n) => {
22+
let e = document.head.querySelector("meta[csrf]");
23+
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));
24+
}, h = async () => {
25+
window.axios.defaults.headers = {
26+
"Content-Type": "multipart/form-data"
27+
}, window.axios.interceptors.request.use(async (t) => {
28+
if (t.method === "post" || t.method === "put") {
29+
let n = r();
30+
n || (o = await s(), o.isGuest || (c(o.csrfTokenName, o.csrfTokenValue), n = r()));
31+
const e = n || o;
32+
if (!e)
33+
throw new Error("CSRF token not found");
34+
const a = l(t.url);
35+
t.url = "", t.data instanceof FormData ? (t.data.append(e.csrfTokenName, e.csrfTokenValue), t.data.append("action", a)) : t.data = {
36+
[e.csrfTokenName]: e.csrfTokenValue,
37+
action: a,
38+
...t.data
39+
};
40+
}
41+
return t;
42+
}), window.axios.interceptors.response.use(
43+
async (t) => (t.config.data.get("action") == "users/login" && await s().then((n) => {
44+
c(n.csrfTokenName, n.csrfTokenValue);
45+
}), t)
46+
);
47+
}, d = async () => {
48+
let n = 0;
49+
const e = setInterval(async () => {
50+
if (window.axios) {
51+
clearInterval(e), await h();
52+
return;
53+
}
54+
n++, n >= 40 && (clearInterval(e), console.warn(
55+
"Inertia (Craft): Axios not found after 10 seconds. CSRF protection may not be active."
56+
));
57+
}, 250);
58+
};
59+
d();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(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

Comments
 (0)