strapi-custom-auth 1.2.4 → 1.2.5

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;
@@ -27,6 +27,101 @@ 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
+ let modifiedBody = init.body;
90
+ if (init.body && typeof init.body === "string") {
91
+ try {
92
+ modifiedBody = JSON.parse(init.body);
93
+ } catch (e) {
94
+ }
95
+ }
96
+ const modifiedInit = {
97
+ ...init,
98
+ headers: {
99
+ ...init.headers,
100
+ Authorization: `Bearer ${token}`,
101
+ "Content-Type": "application/json"
102
+ },
103
+ body: modifiedBody ? JSON.stringify(modifiedBody) : void 0
104
+ };
105
+ try {
106
+ const response = await originalFetch(input, modifiedInit);
107
+ if (!response.ok) {
108
+ const errorData = await response.json();
109
+ console.log("Fetch error:", errorData.error?.message);
110
+ if (response.status === 401) {
111
+ storage.clear();
112
+ if (!window.location.pathname.includes("/admin/auth/login")) {
113
+ window.location.href = "/admin/auth/login";
114
+ }
115
+ }
116
+ throw new Error("Request failed");
117
+ }
118
+ return response;
119
+ } catch (error) {
120
+ console.error("Fetch request failed:", error);
121
+ throw error;
122
+ }
123
+ };
124
+ };
30
125
  const index = {
31
126
  register(app) {
32
127
  app.addMenuLink({
@@ -65,6 +160,20 @@ const index = {
65
160
  }))
66
161
  }
67
162
  });
163
+ app.addMenuLink({
164
+ to: "/custom-logout",
165
+ icon: icons.SignOut,
166
+ intlLabel: {
167
+ id: "custom-logout.label",
168
+ defaultMessage: "Logout"
169
+ },
170
+ Component: async () => {
171
+ const component = await Promise.resolve().then(() => require("../_chunks/CustomLogoutPage-CuntyG5p.js"));
172
+ return component.default;
173
+ }
174
+ });
175
+ const storage = localStorage.getItem("jwtToken") ? localStorage : sessionStorage;
176
+ fetchInterceptor(storage);
68
177
  },
69
178
  async registerTrads({ locales }) {
70
179
  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,101 @@ 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
+ let modifiedBody = init.body;
89
+ if (init.body && typeof init.body === "string") {
90
+ try {
91
+ modifiedBody = JSON.parse(init.body);
92
+ } catch (e) {
93
+ }
94
+ }
95
+ const modifiedInit = {
96
+ ...init,
97
+ headers: {
98
+ ...init.headers,
99
+ Authorization: `Bearer ${token}`,
100
+ "Content-Type": "application/json"
101
+ },
102
+ body: modifiedBody ? JSON.stringify(modifiedBody) : void 0
103
+ };
104
+ try {
105
+ const response = await originalFetch(input, modifiedInit);
106
+ if (!response.ok) {
107
+ const errorData = await response.json();
108
+ console.log("Fetch error:", errorData.error?.message);
109
+ if (response.status === 401) {
110
+ storage.clear();
111
+ if (!window.location.pathname.includes("/admin/auth/login")) {
112
+ window.location.href = "/admin/auth/login";
113
+ }
114
+ }
115
+ throw new Error("Request failed");
116
+ }
117
+ return response;
118
+ } catch (error) {
119
+ console.error("Fetch request failed:", error);
120
+ throw error;
121
+ }
122
+ };
123
+ };
29
124
  const index = {
30
125
  register(app) {
31
126
  app.addMenuLink({
@@ -64,6 +159,20 @@ const index = {
64
159
  }))
65
160
  }
66
161
  });
162
+ app.addMenuLink({
163
+ to: "/custom-logout",
164
+ icon: SignOut,
165
+ intlLabel: {
166
+ id: "custom-logout.label",
167
+ defaultMessage: "Logout"
168
+ },
169
+ Component: async () => {
170
+ const component = await import("../_chunks/CustomLogoutPage-Be-GcZA3.mjs");
171
+ return component.default;
172
+ }
173
+ });
174
+ const storage = localStorage.getItem("jwtToken") ? localStorage : sessionStorage;
175
+ fetchInterceptor(storage);
67
176
  },
68
177
  async registerTrads({ locales }) {
69
178
  return Promise.all(