@kawaiininja/fetch 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/useCsrf.d.ts +1 -1
- package/dist/hooks/useCsrf.js +46 -3
- package/dist/hooks/useFetch.js +22 -10
- package/package.json +1 -1
package/dist/hooks/useCsrf.d.ts
CHANGED
package/dist/hooks/useCsrf.js
CHANGED
|
@@ -1,27 +1,59 @@
|
|
|
1
|
+
import { SecureStoragePlugin } from "capacitor-secure-storage-plugin";
|
|
1
2
|
import { useCallback, useRef } from "react";
|
|
2
3
|
import { INTERNAL_HEADER } from "../context/ApiContext";
|
|
3
4
|
import { useApiConfig } from "./useApiConfig";
|
|
4
5
|
export function useCsrf() {
|
|
5
6
|
const { apiUrl } = useApiConfig();
|
|
6
7
|
const csrfRef = useRef(null);
|
|
7
|
-
const clearCsrf = useCallback(() => {
|
|
8
|
+
const clearCsrf = useCallback(async () => {
|
|
9
|
+
console.log("[useCsrf] Clearing CSRF token...");
|
|
8
10
|
csrfRef.current = null;
|
|
9
11
|
if (typeof window !== "undefined") {
|
|
12
|
+
const platform = window.Capacitor?.getPlatform() || "web";
|
|
13
|
+
const isNative = platform === "ios" || platform === "android";
|
|
14
|
+
if (isNative) {
|
|
15
|
+
try {
|
|
16
|
+
await SecureStoragePlugin.remove({ key: "csrf_token" });
|
|
17
|
+
console.log("[useCsrf] Cleared from SecureStorage.");
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
console.warn("[useCsrf] Failed to clear from SecureStorage, trying localStorage:", e);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
10
23
|
localStorage.removeItem("csrf_token");
|
|
11
24
|
}
|
|
12
25
|
}, []);
|
|
13
26
|
const fetchCSRF = useCallback(async () => {
|
|
14
27
|
if (csrfRef.current)
|
|
15
28
|
return csrfRef.current;
|
|
16
|
-
// Try to get from
|
|
29
|
+
// Try to get from storage first (for Capacitor/mobile)
|
|
17
30
|
if (typeof window !== "undefined") {
|
|
31
|
+
const platform = window.Capacitor?.getPlatform() || "web";
|
|
32
|
+
const isNative = platform === "ios" || platform === "android";
|
|
33
|
+
if (isNative) {
|
|
34
|
+
try {
|
|
35
|
+
const { value } = await SecureStoragePlugin.get({
|
|
36
|
+
key: "csrf_token",
|
|
37
|
+
});
|
|
38
|
+
if (value) {
|
|
39
|
+
console.log("[useCsrf] Retrieved from SecureStorage.");
|
|
40
|
+
csrfRef.current = value;
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
console.warn("[useCsrf] SecureStorage check failed:", e);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
18
48
|
const storedCsrf = localStorage.getItem("csrf_token");
|
|
19
49
|
if (storedCsrf) {
|
|
50
|
+
console.log("[useCsrf] Retrieved from localStorage.");
|
|
20
51
|
csrfRef.current = storedCsrf;
|
|
21
52
|
return storedCsrf;
|
|
22
53
|
}
|
|
23
54
|
}
|
|
24
55
|
// Construct CSRF URL using the context's baseUrl
|
|
56
|
+
console.log("[useCsrf] Fetching new CSRF token from server...");
|
|
25
57
|
const csrfUrl = apiUrl("auth/csrf-token");
|
|
26
58
|
const res = await fetch(csrfUrl, {
|
|
27
59
|
method: "GET",
|
|
@@ -33,8 +65,19 @@ export function useCsrf() {
|
|
|
33
65
|
if (!token)
|
|
34
66
|
throw new Error("Missing CSRF token");
|
|
35
67
|
csrfRef.current = token;
|
|
36
|
-
// Store in
|
|
68
|
+
// Store in storage
|
|
37
69
|
if (typeof window !== "undefined") {
|
|
70
|
+
const platform = window.Capacitor?.getPlatform() || "web";
|
|
71
|
+
const isNative = platform === "ios" || platform === "android";
|
|
72
|
+
if (isNative) {
|
|
73
|
+
try {
|
|
74
|
+
await SecureStoragePlugin.set({ key: "csrf_token", value: token });
|
|
75
|
+
console.log("[useCsrf] Stored in SecureStorage.");
|
|
76
|
+
}
|
|
77
|
+
catch (e) {
|
|
78
|
+
console.warn("[useCsrf] SecureStorage set failed, falling back to localStorage:", e);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
38
81
|
localStorage.setItem("csrf_token", token);
|
|
39
82
|
}
|
|
40
83
|
return token;
|
package/dist/hooks/useFetch.js
CHANGED
|
@@ -51,30 +51,42 @@ export const useFetch = (endpoint, baseOptions = {}) => {
|
|
|
51
51
|
// 🔒 SECURITY STRATEGY: NATIVE (MOBILE)
|
|
52
52
|
if (isNative && isInternal) {
|
|
53
53
|
// Mobile relies on manual headers ("Active Courier")
|
|
54
|
-
// We DO NOT use CSRF tokens on mobile (Cookies generally don't work reliably here)
|
|
55
54
|
// 🛡️ S-RANK UPGRADE: Use Secure Storage (Async) instead of LocalStorage
|
|
55
|
+
console.log(`[useFetch] [Native] Strategy: Active Courier for ${url}`);
|
|
56
|
+
let authToken = null;
|
|
57
|
+
let sessionId = null;
|
|
56
58
|
try {
|
|
57
|
-
const { value:
|
|
59
|
+
const { value: secureAuthToken } = await SecureStoragePlugin.get({
|
|
58
60
|
key: "token",
|
|
59
|
-
})
|
|
60
|
-
const { value:
|
|
61
|
+
});
|
|
62
|
+
const { value: secureSessionId } = await SecureStoragePlugin.get({
|
|
61
63
|
key: "session_id",
|
|
62
|
-
})
|
|
64
|
+
});
|
|
65
|
+
authToken = secureAuthToken;
|
|
66
|
+
sessionId = secureSessionId;
|
|
63
67
|
if (authToken)
|
|
64
|
-
|
|
65
|
-
if (sessionId)
|
|
66
|
-
headersConfig["X-Session-ID"] = sessionId;
|
|
68
|
+
console.log("[useFetch] [Native] Token retrieved from SecureStorage.");
|
|
67
69
|
}
|
|
68
70
|
catch (err) {
|
|
69
|
-
|
|
71
|
+
console.warn("[useFetch] [Native] SecureStorage failed or not present, trying localStorage:", err);
|
|
72
|
+
authToken = localStorage.getItem("token");
|
|
73
|
+
sessionId = localStorage.getItem("session_id");
|
|
74
|
+
if (authToken)
|
|
75
|
+
console.log("[useFetch] [Native] Token retrieved from localStorage fallback.");
|
|
70
76
|
}
|
|
77
|
+
if (authToken)
|
|
78
|
+
headersConfig["Authorization"] = `Bearer ${authToken}`;
|
|
79
|
+
if (sessionId)
|
|
80
|
+
headersConfig["X-Session-ID"] = sessionId;
|
|
71
81
|
}
|
|
72
82
|
// 🔒 SECURITY STRATEGY: WEB (BROWSER)
|
|
73
83
|
if (!isNative && isInternal) {
|
|
74
84
|
// Web relies on Cookies ("Passive Courier") for Auth
|
|
75
85
|
// BUT we MUST attach the CSRF Token to prevent attacks
|
|
76
|
-
if (token)
|
|
86
|
+
if (token) {
|
|
77
87
|
headersConfig["X-CSRF-Token"] = token;
|
|
88
|
+
// console.log("[useFetch] [Web] CSRF token attached.");
|
|
89
|
+
}
|
|
78
90
|
// On Web, we DO NOT attach 'Authorization' or 'Session-ID' from localStorage
|
|
79
91
|
// This minimizes XSS risk. The HttpOnly cookie handles it.
|
|
80
92
|
}
|