strapi-custom-auth 1.1.27 → 1.2.0

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.
@@ -0,0 +1,25 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import "react";
3
+ const CustomLogoutPage = () => {
4
+ const handleLogout = async () => {
5
+ const storage = localStorage.getItem("jwtToken") ? localStorage : sessionStorage;
6
+ const url = `/strapi-custom-auth/${storage.getItem("provider")}/logout`;
7
+ try {
8
+ const fetchResponse = await fetch(url, {
9
+ method: "GET",
10
+ headers: {
11
+ "Content-Type": "application/json"
12
+ }
13
+ });
14
+ const response = await fetchResponse.json();
15
+ storage.clear();
16
+ window.location.href = response.url;
17
+ } catch (e) {
18
+ console.log("error", e.message);
19
+ }
20
+ };
21
+ return /* @__PURE__ */ jsx("div", { style: { padding: "20px" }, children: /* @__PURE__ */ jsx("button", { onClick: handleLogout, style: { padding: "10px 20px", backgroundColor: "#4945ff", color: "#fff", border: "1px solid #4945ff", borderRadius: "5px", cursor: "pointer" }, children: "Logout" }) });
22
+ };
23
+ export {
24
+ CustomLogoutPage as default
25
+ };
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("react/jsx-runtime");
4
+ require("react");
5
+ const CustomLogoutPage = () => {
6
+ const handleLogout = async () => {
7
+ const storage = localStorage.getItem("jwtToken") ? localStorage : sessionStorage;
8
+ const url = `/strapi-custom-auth/${storage.getItem("provider")}/logout`;
9
+ try {
10
+ const fetchResponse = await fetch(url, {
11
+ method: "GET",
12
+ headers: {
13
+ "Content-Type": "application/json"
14
+ }
15
+ });
16
+ const response = await fetchResponse.json();
17
+ storage.clear();
18
+ window.location.href = response.url;
19
+ } catch (e) {
20
+ console.log("error", e.message);
21
+ }
22
+ };
23
+ return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { padding: "20px" }, children: /* @__PURE__ */ jsxRuntime.jsx("button", { onClick: handleLogout, style: { padding: "10px 20px", backgroundColor: "#4945ff", color: "#fff", border: "1px solid #4945ff", borderRadius: "5px", cursor: "pointer" }, children: "Logout" }) });
24
+ };
25
+ exports.default = CustomLogoutPage;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("react/jsx-runtime");
4
+ const React = require("react");
5
+ require("react-intl");
6
+ const designSystem = require("@strapi/design-system");
7
+ const DynamicExpenses = ({
8
+ description,
9
+ disabled,
10
+ error,
11
+ intlLabel,
12
+ name,
13
+ onChange,
14
+ placeholder,
15
+ required,
16
+ value
17
+ }) => {
18
+ const [selection, setSelection] = React.useState("nessuna");
19
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { fontSize: "12px" }, children: [
20
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontWeight: "bold", fontSize: "12px", marginBottom: "8px" }, children: name }),
21
+ /* @__PURE__ */ jsxRuntime.jsx("div", { style: { display: "flex", justifyContent: "start", gap: "15px", marginBottom: "18px" }, children: ["nessuna", "percentuali", "fisse"].map((option) => /* @__PURE__ */ jsxRuntime.jsxs(
22
+ "label",
23
+ {
24
+ style: { display: "flex", alignItems: "center", gap: "5px", fontSize: "12px" },
25
+ children: [
26
+ /* @__PURE__ */ jsxRuntime.jsx(
27
+ "input",
28
+ {
29
+ type: "radio",
30
+ name: `${name}-type`,
31
+ value: option,
32
+ checked: selection === option,
33
+ onChange: (e) => {
34
+ setSelection(e.target.value);
35
+ onChange({ target: { name, value: null } });
36
+ },
37
+ style: { cursor: "pointer" }
38
+ }
39
+ ),
40
+ option.charAt(0).toUpperCase() + option.slice(1)
41
+ ]
42
+ },
43
+ option
44
+ )) }),
45
+ selection !== "nessuna" && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
46
+ /* @__PURE__ */ jsxRuntime.jsxs("p", { style: { marginBottom: "8px", fontSize: "12px" }, children: [
47
+ selection === "percentuali" ? "Percentuale" : "Importo",
48
+ ":"
49
+ ] }),
50
+ selection === "percentuali" && /* @__PURE__ */ jsxRuntime.jsx(
51
+ designSystem.NumberInput,
52
+ {
53
+ disabled,
54
+ error,
55
+ hint: description ? formatMessage(description) : "",
56
+ label: "Percentuale",
57
+ name,
58
+ onValueChange: (value2) => {
59
+ onChange({ target: { name, value: value2 ? `p:${value2}` : null } });
60
+ },
61
+ placeholder: "",
62
+ required,
63
+ step: 0.01,
64
+ value: value ? +value.replace("p:", "") : void 0
65
+ }
66
+ ),
67
+ selection === "fisse" && /* @__PURE__ */ jsxRuntime.jsx(
68
+ designSystem.NumberInput,
69
+ {
70
+ disabled,
71
+ error,
72
+ hint: description ? formatMessage(description) : "",
73
+ label: "Importo",
74
+ name,
75
+ onValueChange: (value2) => {
76
+ onChange({ target: { name, value: value2 ? `p:${value2}` : null } });
77
+ },
78
+ step: 0.01,
79
+ placeholder: "",
80
+ required,
81
+ value: value ? +value.replace("p:", "") : void 0
82
+ }
83
+ )
84
+ ] })
85
+ ] });
86
+ };
87
+ exports.DynamicExpenses = DynamicExpenses;
@@ -0,0 +1,87 @@
1
+ import { jsxs, jsx, Fragment } from "react/jsx-runtime";
2
+ import { useState } from "react";
3
+ import "react-intl";
4
+ import { NumberInput } from "@strapi/design-system";
5
+ const DynamicExpenses = ({
6
+ description,
7
+ disabled,
8
+ error,
9
+ intlLabel,
10
+ name,
11
+ onChange,
12
+ placeholder,
13
+ required,
14
+ value
15
+ }) => {
16
+ const [selection, setSelection] = useState("nessuna");
17
+ return /* @__PURE__ */ jsxs("div", { style: { fontSize: "12px" }, children: [
18
+ /* @__PURE__ */ jsx("div", { style: { fontWeight: "bold", fontSize: "12px", marginBottom: "8px" }, children: name }),
19
+ /* @__PURE__ */ jsx("div", { style: { display: "flex", justifyContent: "start", gap: "15px", marginBottom: "18px" }, children: ["nessuna", "percentuali", "fisse"].map((option) => /* @__PURE__ */ jsxs(
20
+ "label",
21
+ {
22
+ style: { display: "flex", alignItems: "center", gap: "5px", fontSize: "12px" },
23
+ children: [
24
+ /* @__PURE__ */ jsx(
25
+ "input",
26
+ {
27
+ type: "radio",
28
+ name: `${name}-type`,
29
+ value: option,
30
+ checked: selection === option,
31
+ onChange: (e) => {
32
+ setSelection(e.target.value);
33
+ onChange({ target: { name, value: null } });
34
+ },
35
+ style: { cursor: "pointer" }
36
+ }
37
+ ),
38
+ option.charAt(0).toUpperCase() + option.slice(1)
39
+ ]
40
+ },
41
+ option
42
+ )) }),
43
+ selection !== "nessuna" && /* @__PURE__ */ jsxs(Fragment, { children: [
44
+ /* @__PURE__ */ jsxs("p", { style: { marginBottom: "8px", fontSize: "12px" }, children: [
45
+ selection === "percentuali" ? "Percentuale" : "Importo",
46
+ ":"
47
+ ] }),
48
+ selection === "percentuali" && /* @__PURE__ */ jsx(
49
+ NumberInput,
50
+ {
51
+ disabled,
52
+ error,
53
+ hint: description ? formatMessage(description) : "",
54
+ label: "Percentuale",
55
+ name,
56
+ onValueChange: (value2) => {
57
+ onChange({ target: { name, value: value2 ? `p:${value2}` : null } });
58
+ },
59
+ placeholder: "",
60
+ required,
61
+ step: 0.01,
62
+ value: value ? +value.replace("p:", "") : void 0
63
+ }
64
+ ),
65
+ selection === "fisse" && /* @__PURE__ */ jsx(
66
+ NumberInput,
67
+ {
68
+ disabled,
69
+ error,
70
+ hint: description ? formatMessage(description) : "",
71
+ label: "Importo",
72
+ name,
73
+ onValueChange: (value2) => {
74
+ onChange({ target: { name, value: value2 ? `p:${value2}` : null } });
75
+ },
76
+ step: 0.01,
77
+ placeholder: "",
78
+ required,
79
+ value: value ? +value.replace("p:", "") : void 0
80
+ }
81
+ )
82
+ ] })
83
+ ] });
84
+ };
85
+ export {
86
+ DynamicExpenses
87
+ };
@@ -27,6 +27,92 @@ const Initializer = ({ setPlugin }) => {
27
27
  return null;
28
28
  };
29
29
  const PluginIcon = () => /* @__PURE__ */ jsxRuntime.jsx(icons.PuzzlePiece, {});
30
+ const isTokenExpired = (token) => {
31
+ if (!token) return true;
32
+ try {
33
+ const payload = JSON.parse(atob(token.split(".")[1]));
34
+ return payload.exp * 1e3 < Date.now();
35
+ } catch (error) {
36
+ console.error("Error decoding token:", error);
37
+ return true;
38
+ }
39
+ };
40
+ const renewToken = async (storage, originalFetch2) => {
41
+ const refreshToken = storage.getItem("refreshToken");
42
+ if (!refreshToken) {
43
+ throw new Error("No refresh token available");
44
+ }
45
+ try {
46
+ const url = `/strapi-custom-auth/${storage.getItem("provider")}/renew-token`;
47
+ const response = await originalFetch2(url, {
48
+ method: "POST",
49
+ headers: {
50
+ "Content-Type": "application/json"
51
+ },
52
+ body: JSON.stringify({ refreshToken })
53
+ });
54
+ if (!response.ok) {
55
+ const errorData = await response.json();
56
+ console.log("Fetch error:", errorData.error?.message);
57
+ throw new Error("Failed to renew token");
58
+ }
59
+ const data = await response.json();
60
+ storage.setItem("jwtToken", `"${data.accessToken}"`);
61
+ storage.setItem("refreshToken", `"${data.refreshToken}"`);
62
+ return data.accessToken;
63
+ } catch (error) {
64
+ console.log("Error renewing token:", error.message);
65
+ throw error;
66
+ }
67
+ };
68
+ const originalFetch = window.fetch;
69
+ const fetchInterceptor = (storage) => {
70
+ window.fetch = async (input, init = {}) => {
71
+ const url = input instanceof Request ? input.url : input;
72
+ if (url.includes("/admin/renew-token") || url.includes("/auth/login") || url.includes("repos/strapi/strapi/releases/latest")) {
73
+ return originalFetch(input, init);
74
+ }
75
+ let token = storage.getItem("jwtToken").replace(/"/g, "");
76
+ if (isTokenExpired(token)) {
77
+ try {
78
+ token = await renewToken(storage, originalFetch);
79
+ console.log("Token renewed successfully");
80
+ } catch (error) {
81
+ console.error("Token renewal failed. Logging out...");
82
+ storage.clear();
83
+ if (!window.location.pathname.includes("/admin/auth/login")) {
84
+ window.location.href = "/admin/auth/login";
85
+ }
86
+ return Promise.reject(error);
87
+ }
88
+ }
89
+ const modifiedInit = {
90
+ ...init,
91
+ headers: {
92
+ ...init.headers,
93
+ Authorization: `Bearer ${token}`
94
+ }
95
+ };
96
+ try {
97
+ const response = await originalFetch(input, modifiedInit);
98
+ if (!response.ok) {
99
+ const errorData = await response.json();
100
+ console.log("Fetch error:", errorData.error?.message);
101
+ if (response.status === 401) {
102
+ storage.clear();
103
+ if (!window.location.pathname.includes("/admin/auth/login")) {
104
+ window.location.href = "/admin/auth/login";
105
+ }
106
+ }
107
+ throw new Error("Request failed");
108
+ }
109
+ return response;
110
+ } catch (error) {
111
+ console.error("Fetch request failed:", error);
112
+ throw error;
113
+ }
114
+ };
115
+ };
30
116
  const index = {
31
117
  register(app) {
32
118
  app.addMenuLink({
@@ -47,6 +133,38 @@ const index = {
47
133
  isReady: false,
48
134
  name: PLUGIN_ID
49
135
  });
136
+ app.customFields.register({
137
+ name: "dynamic-expenses",
138
+ pluginId: `${PLUGIN_ID}`,
139
+ type: "string",
140
+ intlLabel: {
141
+ id: `${PLUGIN_ID}.dynamic-expenses.label`,
142
+ defaultMessage: "Dynamic Expenses"
143
+ },
144
+ intlDescription: {
145
+ id: `${PLUGIN_ID}.dynamic-expenses.description`,
146
+ defaultMessage: "Create your Dynamic Expenses"
147
+ },
148
+ components: {
149
+ Input: async () => Promise.resolve().then(() => require("../_chunks/DynamicExpenses-CagldSh6.js")).then((module2) => ({
150
+ default: module2.DynamicExpenses
151
+ }))
152
+ }
153
+ });
154
+ app.addMenuLink({
155
+ to: "/custom-logout",
156
+ icon: icons.SignOut,
157
+ intlLabel: {
158
+ id: "custom-logout.label",
159
+ defaultMessage: "Logout"
160
+ },
161
+ Component: async () => {
162
+ const component = await Promise.resolve().then(() => require("../_chunks/CustomLogoutPage-CuntyG5p.js"));
163
+ return component.default;
164
+ }
165
+ });
166
+ const storage = localStorage.getItem("jwtToken") ? localStorage : sessionStorage;
167
+ fetchInterceptor(storage);
50
168
  },
51
169
  async registerTrads({ locales }) {
52
170
  return Promise.all(
@@ -1,6 +1,6 @@
1
1
  import { useRef, useEffect } from "react";
2
2
  import { jsx } from "react/jsx-runtime";
3
- import { PuzzlePiece } from "@strapi/icons";
3
+ import { PuzzlePiece, SignOut } from "@strapi/icons";
4
4
  const __variableDynamicImportRuntimeHelper = (glob, path, segs) => {
5
5
  const v = glob[path];
6
6
  if (v) {
@@ -26,6 +26,92 @@ const Initializer = ({ setPlugin }) => {
26
26
  return null;
27
27
  };
28
28
  const PluginIcon = () => /* @__PURE__ */ jsx(PuzzlePiece, {});
29
+ const isTokenExpired = (token) => {
30
+ if (!token) return true;
31
+ try {
32
+ const payload = JSON.parse(atob(token.split(".")[1]));
33
+ return payload.exp * 1e3 < Date.now();
34
+ } catch (error) {
35
+ console.error("Error decoding token:", error);
36
+ return true;
37
+ }
38
+ };
39
+ const renewToken = async (storage, originalFetch2) => {
40
+ const refreshToken = storage.getItem("refreshToken");
41
+ if (!refreshToken) {
42
+ throw new Error("No refresh token available");
43
+ }
44
+ try {
45
+ const url = `/strapi-custom-auth/${storage.getItem("provider")}/renew-token`;
46
+ const response = await originalFetch2(url, {
47
+ method: "POST",
48
+ headers: {
49
+ "Content-Type": "application/json"
50
+ },
51
+ body: JSON.stringify({ refreshToken })
52
+ });
53
+ if (!response.ok) {
54
+ const errorData = await response.json();
55
+ console.log("Fetch error:", errorData.error?.message);
56
+ throw new Error("Failed to renew token");
57
+ }
58
+ const data = await response.json();
59
+ storage.setItem("jwtToken", `"${data.accessToken}"`);
60
+ storage.setItem("refreshToken", `"${data.refreshToken}"`);
61
+ return data.accessToken;
62
+ } catch (error) {
63
+ console.log("Error renewing token:", error.message);
64
+ throw error;
65
+ }
66
+ };
67
+ const originalFetch = window.fetch;
68
+ const fetchInterceptor = (storage) => {
69
+ window.fetch = async (input, init = {}) => {
70
+ const url = input instanceof Request ? input.url : input;
71
+ if (url.includes("/admin/renew-token") || url.includes("/auth/login") || url.includes("repos/strapi/strapi/releases/latest")) {
72
+ return originalFetch(input, init);
73
+ }
74
+ let token = storage.getItem("jwtToken").replace(/"/g, "");
75
+ if (isTokenExpired(token)) {
76
+ try {
77
+ token = await renewToken(storage, originalFetch);
78
+ console.log("Token renewed successfully");
79
+ } catch (error) {
80
+ console.error("Token renewal failed. Logging out...");
81
+ storage.clear();
82
+ if (!window.location.pathname.includes("/admin/auth/login")) {
83
+ window.location.href = "/admin/auth/login";
84
+ }
85
+ return Promise.reject(error);
86
+ }
87
+ }
88
+ const modifiedInit = {
89
+ ...init,
90
+ headers: {
91
+ ...init.headers,
92
+ Authorization: `Bearer ${token}`
93
+ }
94
+ };
95
+ try {
96
+ const response = await originalFetch(input, modifiedInit);
97
+ if (!response.ok) {
98
+ const errorData = await response.json();
99
+ console.log("Fetch error:", errorData.error?.message);
100
+ if (response.status === 401) {
101
+ storage.clear();
102
+ if (!window.location.pathname.includes("/admin/auth/login")) {
103
+ window.location.href = "/admin/auth/login";
104
+ }
105
+ }
106
+ throw new Error("Request failed");
107
+ }
108
+ return response;
109
+ } catch (error) {
110
+ console.error("Fetch request failed:", error);
111
+ throw error;
112
+ }
113
+ };
114
+ };
29
115
  const index = {
30
116
  register(app) {
31
117
  app.addMenuLink({
@@ -46,6 +132,38 @@ const index = {
46
132
  isReady: false,
47
133
  name: PLUGIN_ID
48
134
  });
135
+ app.customFields.register({
136
+ name: "dynamic-expenses",
137
+ pluginId: `${PLUGIN_ID}`,
138
+ type: "string",
139
+ intlLabel: {
140
+ id: `${PLUGIN_ID}.dynamic-expenses.label`,
141
+ defaultMessage: "Dynamic Expenses"
142
+ },
143
+ intlDescription: {
144
+ id: `${PLUGIN_ID}.dynamic-expenses.description`,
145
+ defaultMessage: "Create your Dynamic Expenses"
146
+ },
147
+ components: {
148
+ Input: async () => import("../_chunks/DynamicExpenses-DJdLL78e.mjs").then((module) => ({
149
+ default: module.DynamicExpenses
150
+ }))
151
+ }
152
+ });
153
+ app.addMenuLink({
154
+ to: "/custom-logout",
155
+ icon: SignOut,
156
+ intlLabel: {
157
+ id: "custom-logout.label",
158
+ defaultMessage: "Logout"
159
+ },
160
+ Component: async () => {
161
+ const component = await import("../_chunks/CustomLogoutPage-Be-GcZA3.mjs");
162
+ return component.default;
163
+ }
164
+ });
165
+ const storage = localStorage.getItem("jwtToken") ? localStorage : sessionStorage;
166
+ fetchInterceptor(storage);
49
167
  },
50
168
  async registerTrads({ locales }) {
51
169
  return Promise.all(