nextauthz 1.3.9 → 1.3.11

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/index.js CHANGED
@@ -28,8 +28,186 @@ __export(index_exports, {
28
28
  module.exports = __toCommonJS(index_exports);
29
29
 
30
30
  // src/AuthProvider.tsx
31
+ var import_react2 = require("react");
32
+
33
+ // node_modules/jwt-decode/build/esm/index.js
34
+ var esm_exports = {};
35
+ __export(esm_exports, {
36
+ InvalidTokenError: () => InvalidTokenError,
37
+ jwtDecode: () => jwtDecode
38
+ });
39
+ var InvalidTokenError = class extends Error {
40
+ };
41
+ InvalidTokenError.prototype.name = "InvalidTokenError";
42
+ function b64DecodeUnicode(str) {
43
+ return decodeURIComponent(atob(str).replace(/(.)/g, (m, p) => {
44
+ let code = p.charCodeAt(0).toString(16).toUpperCase();
45
+ if (code.length < 2) {
46
+ code = "0" + code;
47
+ }
48
+ return "%" + code;
49
+ }));
50
+ }
51
+ function base64UrlDecode(str) {
52
+ let output = str.replace(/-/g, "+").replace(/_/g, "/");
53
+ switch (output.length % 4) {
54
+ case 0:
55
+ break;
56
+ case 2:
57
+ output += "==";
58
+ break;
59
+ case 3:
60
+ output += "=";
61
+ break;
62
+ default:
63
+ throw new Error("base64 string is not of the correct length");
64
+ }
65
+ try {
66
+ return b64DecodeUnicode(output);
67
+ } catch (err) {
68
+ return atob(output);
69
+ }
70
+ }
71
+ function jwtDecode(token, options) {
72
+ if (typeof token !== "string") {
73
+ throw new InvalidTokenError("Invalid token specified: must be a string");
74
+ }
75
+ options || (options = {});
76
+ const pos = options.header === true ? 0 : 1;
77
+ const part = token.split(".")[pos];
78
+ if (typeof part !== "string") {
79
+ throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
80
+ }
81
+ let decoded;
82
+ try {
83
+ decoded = base64UrlDecode(part);
84
+ } catch (e) {
85
+ throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`);
86
+ }
87
+ try {
88
+ return JSON.parse(decoded);
89
+ } catch (e) {
90
+ throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`);
91
+ }
92
+ }
93
+
94
+ // node_modules/react-token-manager/dist/index.mjs
31
95
  var import_react = require("react");
32
- var import_react_token_manager = require("react-token-manager");
96
+ var jwtDecode2 = void 0 || esm_exports;
97
+ var globalOptions = {
98
+ storage: "localStorage"
99
+ };
100
+ var configureTokenManager = (options) => {
101
+ globalOptions = { ...globalOptions, ...options };
102
+ };
103
+ var TokenManager = class {
104
+ constructor(options) {
105
+ this.trackedKeys = /* @__PURE__ */ new Set();
106
+ const opts = options || globalOptions;
107
+ this.storage = opts.storage || "localStorage";
108
+ }
109
+ /** Set multiple tokens at once */
110
+ set(tokens) {
111
+ Object.entries(tokens).forEach(([key, value]) => {
112
+ this.trackedKeys.add(key);
113
+ if (this.storage === "localStorage") localStorage.setItem(key, value);
114
+ else if (this.storage === "sessionStorage") sessionStorage.setItem(key, value);
115
+ else document.cookie = `${key}=${value}; path=/`;
116
+ });
117
+ }
118
+ /** Get token(s) by key(s) */
119
+ get(keys) {
120
+ const keyArray = Array.isArray(keys) ? keys : [keys];
121
+ const result = {};
122
+ keyArray.forEach((key) => {
123
+ result[key] = this.getOne(key);
124
+ });
125
+ return result;
126
+ }
127
+ /** Get a single token by key */
128
+ getOne(key) {
129
+ if (this.storage === "localStorage") return localStorage.getItem(key);
130
+ if (this.storage === "sessionStorage") return sessionStorage.getItem(key);
131
+ const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
132
+ return match ? match[2] : null;
133
+ }
134
+ /** Get all tracked tokens */
135
+ getAll() {
136
+ const result = {};
137
+ this.trackedKeys.forEach((key) => {
138
+ result[key] = this.getOne(key);
139
+ });
140
+ return result;
141
+ }
142
+ /** Remove specific tokens */
143
+ remove(keys) {
144
+ const keyArray = Array.isArray(keys) ? keys : [keys];
145
+ keyArray.forEach((key) => {
146
+ if (this.storage === "localStorage") localStorage.removeItem(key);
147
+ else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
148
+ else
149
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
150
+ this.trackedKeys.delete(key);
151
+ });
152
+ }
153
+ /** Clear all tracked auth tokens */
154
+ clear() {
155
+ this.trackedKeys.forEach((key) => {
156
+ if (this.storage === "localStorage") localStorage.removeItem(key);
157
+ else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
158
+ else
159
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
160
+ });
161
+ this.trackedKeys.clear();
162
+ }
163
+ /** Decode token */
164
+ decode(token) {
165
+ if (!token) return null;
166
+ try {
167
+ const cleanToken = token.trim().replace(/^"|"$/g, "");
168
+ return jwtDecode2(cleanToken);
169
+ } catch (err) {
170
+ console.error("JWT decode error:", err, token);
171
+ return null;
172
+ }
173
+ }
174
+ /** Check if a token is expired
175
+ * If no token is provided, defaults to checking 'access_token'
176
+ */
177
+ isExpired(token) {
178
+ const tokenToCheck = token != null ? token : this.getOne("access_token");
179
+ console.log("tokenToCheck:", tokenToCheck);
180
+ if (!tokenToCheck) return true;
181
+ const decoded = this.decode(tokenToCheck);
182
+ console.log("decoded:", decoded);
183
+ if (!decoded || !decoded.exp) return true;
184
+ const now = Date.now();
185
+ const expTime = decoded.exp * 1e3;
186
+ const isExpired = now >= expTime;
187
+ console.log("Token expires at:", new Date(expTime), "isExpired:", isExpired);
188
+ return isExpired;
189
+ }
190
+ };
191
+ var useTokenManager = () => {
192
+ const manager = (0, import_react.useMemo)(() => new TokenManager(), []);
193
+ return {
194
+ /** Set multiple tokens */
195
+ setTokens: (tokens) => manager.set(tokens),
196
+ /** Get all tokens by keys */
197
+ getAllTokens: () => manager.getAll(),
198
+ /** Get multiple tokens by keys */
199
+ getTokens: (keys) => manager.get(keys),
200
+ /** Get a single token by key */
201
+ getSingleToken: (key) => manager.getOne(key),
202
+ /** Remove multiple tokens by keys */
203
+ removeTokens: (keys) => manager.remove(keys),
204
+ clearTokens: () => manager.clear(),
205
+ /** Decode a single token */
206
+ decodeToken: (token) => manager.decode(token),
207
+ /** Check expiration of a single token */
208
+ isExpired: (token) => manager.isExpired(token)
209
+ };
210
+ };
33
211
 
34
212
  // store/useGuardStore.ts
35
213
  var import_zustand = require("zustand");
@@ -59,14 +237,14 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
59
237
  // src/AuthProvider.tsx
60
238
  var import_jsx_runtime = require("react/jsx-runtime");
61
239
  function createAuthContext(option) {
62
- const AuthContext = (0, import_react.createContext)(void 0);
240
+ const AuthContext = (0, import_react2.createContext)(void 0);
63
241
  const AuthProvider = ({ children }) => {
64
242
  const storage = option?.storage ?? "cookie";
65
243
  const tokenKey = option?.tokenKey ?? "access_token";
66
- (0, import_react.useEffect)(() => {
67
- (0, import_react_token_manager.configureTokenManager)({ storage });
244
+ (0, import_react2.useEffect)(() => {
245
+ configureTokenManager({ storage });
68
246
  }, [storage]);
69
- const manager = (0, import_react_token_manager.useTokenManager)();
247
+ const manager = useTokenManager();
70
248
  const {
71
249
  user,
72
250
  role,
@@ -77,7 +255,7 @@ function createAuthContext(option) {
77
255
  setAuth,
78
256
  setAuthChecked
79
257
  } = useAuthStore();
80
- (0, import_react.useEffect)(() => {
258
+ (0, import_react2.useEffect)(() => {
81
259
  const storedUser = manager.getSingleToken("user");
82
260
  const token = manager.getSingleToken(tokenKey);
83
261
  const expired = manager.isExpired(token);
@@ -130,7 +308,7 @@ function createAuthContext(option) {
130
308
  );
131
309
  };
132
310
  const useAuth = () => {
133
- const context = (0, import_react.useContext)(AuthContext);
311
+ const context = (0, import_react2.useContext)(AuthContext);
134
312
  if (!context) throw new Error("useAuth must be used within AuthProvider");
135
313
  return context;
136
314
  };
@@ -142,7 +320,7 @@ function createAuthContext(option) {
142
320
  }
143
321
 
144
322
  // src/AuthGuard.tsx
145
- var import_react2 = require("react");
323
+ var import_react3 = require("react");
146
324
  var import_navigation = require("next/navigation");
147
325
  var import_jsx_runtime2 = require("react/jsx-runtime");
148
326
  var AuthGuard = ({
@@ -152,7 +330,7 @@ var AuthGuard = ({
152
330
  }) => {
153
331
  const router = (0, import_navigation.useRouter)();
154
332
  const { isAuthenticated, isAuthChecked } = useAuthStore();
155
- (0, import_react2.useEffect)(() => {
333
+ (0, import_react3.useEffect)(() => {
156
334
  if (!isAuthChecked) return;
157
335
  if (!isAuthenticated) {
158
336
  router.replace(redirectTo);
@@ -165,7 +343,7 @@ var AuthGuard = ({
165
343
  var AuthGuard_default = AuthGuard;
166
344
 
167
345
  // src/RoleGuard.tsx
168
- var import_react3 = require("react");
346
+ var import_react4 = require("react");
169
347
  var import_navigation2 = require("next/navigation");
170
348
  var import_jsx_runtime3 = require("react/jsx-runtime");
171
349
  var RoleGuard = ({
@@ -176,7 +354,7 @@ var RoleGuard = ({
176
354
  }) => {
177
355
  const router = (0, import_navigation2.useRouter)();
178
356
  const { role, isAuthChecked } = useAuthStore();
179
- (0, import_react3.useEffect)(() => {
357
+ (0, import_react4.useEffect)(() => {
180
358
  if (!isAuthChecked) return;
181
359
  if (!role || !allowedRoles.includes(role)) {
182
360
  router.replace(redirectTo);
package/dist/index.mjs CHANGED
@@ -1,8 +1,191 @@
1
1
  "use client";
2
+ var __defProp = Object.defineProperty;
3
+ var __export = (target, all) => {
4
+ for (var name in all)
5
+ __defProp(target, name, { get: all[name], enumerable: true });
6
+ };
2
7
 
3
8
  // src/AuthProvider.tsx
4
9
  import { createContext, useContext, useEffect } from "react";
5
- import { configureTokenManager, useTokenManager } from "react-token-manager";
10
+
11
+ // node_modules/jwt-decode/build/esm/index.js
12
+ var esm_exports = {};
13
+ __export(esm_exports, {
14
+ InvalidTokenError: () => InvalidTokenError,
15
+ jwtDecode: () => jwtDecode
16
+ });
17
+ var InvalidTokenError = class extends Error {
18
+ };
19
+ InvalidTokenError.prototype.name = "InvalidTokenError";
20
+ function b64DecodeUnicode(str) {
21
+ return decodeURIComponent(atob(str).replace(/(.)/g, (m, p) => {
22
+ let code = p.charCodeAt(0).toString(16).toUpperCase();
23
+ if (code.length < 2) {
24
+ code = "0" + code;
25
+ }
26
+ return "%" + code;
27
+ }));
28
+ }
29
+ function base64UrlDecode(str) {
30
+ let output = str.replace(/-/g, "+").replace(/_/g, "/");
31
+ switch (output.length % 4) {
32
+ case 0:
33
+ break;
34
+ case 2:
35
+ output += "==";
36
+ break;
37
+ case 3:
38
+ output += "=";
39
+ break;
40
+ default:
41
+ throw new Error("base64 string is not of the correct length");
42
+ }
43
+ try {
44
+ return b64DecodeUnicode(output);
45
+ } catch (err) {
46
+ return atob(output);
47
+ }
48
+ }
49
+ function jwtDecode(token, options) {
50
+ if (typeof token !== "string") {
51
+ throw new InvalidTokenError("Invalid token specified: must be a string");
52
+ }
53
+ options || (options = {});
54
+ const pos = options.header === true ? 0 : 1;
55
+ const part = token.split(".")[pos];
56
+ if (typeof part !== "string") {
57
+ throw new InvalidTokenError(`Invalid token specified: missing part #${pos + 1}`);
58
+ }
59
+ let decoded;
60
+ try {
61
+ decoded = base64UrlDecode(part);
62
+ } catch (e) {
63
+ throw new InvalidTokenError(`Invalid token specified: invalid base64 for part #${pos + 1} (${e.message})`);
64
+ }
65
+ try {
66
+ return JSON.parse(decoded);
67
+ } catch (e) {
68
+ throw new InvalidTokenError(`Invalid token specified: invalid json for part #${pos + 1} (${e.message})`);
69
+ }
70
+ }
71
+
72
+ // node_modules/react-token-manager/dist/index.mjs
73
+ import { useMemo } from "react";
74
+ var jwtDecode2 = void 0 || esm_exports;
75
+ var globalOptions = {
76
+ storage: "localStorage"
77
+ };
78
+ var configureTokenManager = (options) => {
79
+ globalOptions = { ...globalOptions, ...options };
80
+ };
81
+ var TokenManager = class {
82
+ constructor(options) {
83
+ this.trackedKeys = /* @__PURE__ */ new Set();
84
+ const opts = options || globalOptions;
85
+ this.storage = opts.storage || "localStorage";
86
+ }
87
+ /** Set multiple tokens at once */
88
+ set(tokens) {
89
+ Object.entries(tokens).forEach(([key, value]) => {
90
+ this.trackedKeys.add(key);
91
+ if (this.storage === "localStorage") localStorage.setItem(key, value);
92
+ else if (this.storage === "sessionStorage") sessionStorage.setItem(key, value);
93
+ else document.cookie = `${key}=${value}; path=/`;
94
+ });
95
+ }
96
+ /** Get token(s) by key(s) */
97
+ get(keys) {
98
+ const keyArray = Array.isArray(keys) ? keys : [keys];
99
+ const result = {};
100
+ keyArray.forEach((key) => {
101
+ result[key] = this.getOne(key);
102
+ });
103
+ return result;
104
+ }
105
+ /** Get a single token by key */
106
+ getOne(key) {
107
+ if (this.storage === "localStorage") return localStorage.getItem(key);
108
+ if (this.storage === "sessionStorage") return sessionStorage.getItem(key);
109
+ const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
110
+ return match ? match[2] : null;
111
+ }
112
+ /** Get all tracked tokens */
113
+ getAll() {
114
+ const result = {};
115
+ this.trackedKeys.forEach((key) => {
116
+ result[key] = this.getOne(key);
117
+ });
118
+ return result;
119
+ }
120
+ /** Remove specific tokens */
121
+ remove(keys) {
122
+ const keyArray = Array.isArray(keys) ? keys : [keys];
123
+ keyArray.forEach((key) => {
124
+ if (this.storage === "localStorage") localStorage.removeItem(key);
125
+ else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
126
+ else
127
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
128
+ this.trackedKeys.delete(key);
129
+ });
130
+ }
131
+ /** Clear all tracked auth tokens */
132
+ clear() {
133
+ this.trackedKeys.forEach((key) => {
134
+ if (this.storage === "localStorage") localStorage.removeItem(key);
135
+ else if (this.storage === "sessionStorage") sessionStorage.removeItem(key);
136
+ else
137
+ document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
138
+ });
139
+ this.trackedKeys.clear();
140
+ }
141
+ /** Decode token */
142
+ decode(token) {
143
+ if (!token) return null;
144
+ try {
145
+ const cleanToken = token.trim().replace(/^"|"$/g, "");
146
+ return jwtDecode2(cleanToken);
147
+ } catch (err) {
148
+ console.error("JWT decode error:", err, token);
149
+ return null;
150
+ }
151
+ }
152
+ /** Check if a token is expired
153
+ * If no token is provided, defaults to checking 'access_token'
154
+ */
155
+ isExpired(token) {
156
+ const tokenToCheck = token != null ? token : this.getOne("access_token");
157
+ console.log("tokenToCheck:", tokenToCheck);
158
+ if (!tokenToCheck) return true;
159
+ const decoded = this.decode(tokenToCheck);
160
+ console.log("decoded:", decoded);
161
+ if (!decoded || !decoded.exp) return true;
162
+ const now = Date.now();
163
+ const expTime = decoded.exp * 1e3;
164
+ const isExpired = now >= expTime;
165
+ console.log("Token expires at:", new Date(expTime), "isExpired:", isExpired);
166
+ return isExpired;
167
+ }
168
+ };
169
+ var useTokenManager = () => {
170
+ const manager = useMemo(() => new TokenManager(), []);
171
+ return {
172
+ /** Set multiple tokens */
173
+ setTokens: (tokens) => manager.set(tokens),
174
+ /** Get all tokens by keys */
175
+ getAllTokens: () => manager.getAll(),
176
+ /** Get multiple tokens by keys */
177
+ getTokens: (keys) => manager.get(keys),
178
+ /** Get a single token by key */
179
+ getSingleToken: (key) => manager.getOne(key),
180
+ /** Remove multiple tokens by keys */
181
+ removeTokens: (keys) => manager.remove(keys),
182
+ clearTokens: () => manager.clear(),
183
+ /** Decode a single token */
184
+ decodeToken: (token) => manager.decode(token),
185
+ /** Check expiration of a single token */
186
+ isExpired: (token) => manager.isExpired(token)
187
+ };
188
+ };
6
189
 
7
190
  // store/useGuardStore.ts
8
191
  import { create } from "zustand";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.3.9",
3
+ "version": "1.3.11",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -23,8 +23,5 @@
23
23
  "react": "^18.2.0 || ^19.0.0",
24
24
  "react-dom": "^18.2.0 || ^19.0.0",
25
25
  "zustand": "^5.0.0"
26
- },
27
- "dependencies": {
28
- "react-token-manager": "^1.1.2"
29
26
  }
30
27
  }