nextauthz 1.3.11 → 1.3.13

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,186 +28,8 @@ __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
95
31
  var import_react = require("react");
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
- };
32
+ var import_react_token_manager = require("react-token-manager");
211
33
 
212
34
  // store/useGuardStore.ts
213
35
  var import_zustand = require("zustand");
@@ -237,14 +59,14 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
237
59
  // src/AuthProvider.tsx
238
60
  var import_jsx_runtime = require("react/jsx-runtime");
239
61
  function createAuthContext(option) {
240
- const AuthContext = (0, import_react2.createContext)(void 0);
62
+ const AuthContext = (0, import_react.createContext)(void 0);
241
63
  const AuthProvider = ({ children }) => {
242
64
  const storage = option?.storage ?? "cookie";
243
65
  const tokenKey = option?.tokenKey ?? "access_token";
244
- (0, import_react2.useEffect)(() => {
245
- configureTokenManager({ storage });
66
+ (0, import_react.useEffect)(() => {
67
+ (0, import_react_token_manager.configureTokenManager)({ storage });
246
68
  }, [storage]);
247
- const manager = useTokenManager();
69
+ const manager = (0, import_react_token_manager.useTokenManager)();
248
70
  const {
249
71
  user,
250
72
  role,
@@ -255,11 +77,9 @@ function createAuthContext(option) {
255
77
  setAuth,
256
78
  setAuthChecked
257
79
  } = useAuthStore();
258
- (0, import_react2.useEffect)(() => {
80
+ (0, import_react.useEffect)(() => {
259
81
  const storedUser = manager.getSingleToken("user");
260
82
  const token = manager.getSingleToken(tokenKey);
261
- const expired = manager.isExpired(token);
262
- console.log("is expired", expired);
263
83
  if (token && !manager.isExpired(token)) {
264
84
  try {
265
85
  setAuth(true);
@@ -308,7 +128,7 @@ function createAuthContext(option) {
308
128
  );
309
129
  };
310
130
  const useAuth = () => {
311
- const context = (0, import_react2.useContext)(AuthContext);
131
+ const context = (0, import_react.useContext)(AuthContext);
312
132
  if (!context) throw new Error("useAuth must be used within AuthProvider");
313
133
  return context;
314
134
  };
@@ -320,7 +140,7 @@ function createAuthContext(option) {
320
140
  }
321
141
 
322
142
  // src/AuthGuard.tsx
323
- var import_react3 = require("react");
143
+ var import_react2 = require("react");
324
144
  var import_navigation = require("next/navigation");
325
145
  var import_jsx_runtime2 = require("react/jsx-runtime");
326
146
  var AuthGuard = ({
@@ -330,7 +150,7 @@ var AuthGuard = ({
330
150
  }) => {
331
151
  const router = (0, import_navigation.useRouter)();
332
152
  const { isAuthenticated, isAuthChecked } = useAuthStore();
333
- (0, import_react3.useEffect)(() => {
153
+ (0, import_react2.useEffect)(() => {
334
154
  if (!isAuthChecked) return;
335
155
  if (!isAuthenticated) {
336
156
  router.replace(redirectTo);
@@ -343,7 +163,7 @@ var AuthGuard = ({
343
163
  var AuthGuard_default = AuthGuard;
344
164
 
345
165
  // src/RoleGuard.tsx
346
- var import_react4 = require("react");
166
+ var import_react3 = require("react");
347
167
  var import_navigation2 = require("next/navigation");
348
168
  var import_jsx_runtime3 = require("react/jsx-runtime");
349
169
  var RoleGuard = ({
@@ -354,7 +174,7 @@ var RoleGuard = ({
354
174
  }) => {
355
175
  const router = (0, import_navigation2.useRouter)();
356
176
  const { role, isAuthChecked } = useAuthStore();
357
- (0, import_react4.useEffect)(() => {
177
+ (0, import_react3.useEffect)(() => {
358
178
  if (!isAuthChecked) return;
359
179
  if (!role || !allowedRoles.includes(role)) {
360
180
  router.replace(redirectTo);
package/dist/index.mjs CHANGED
@@ -1,191 +1,8 @@
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
- };
7
2
 
8
3
  // src/AuthProvider.tsx
9
4
  import { createContext, useContext, useEffect } from "react";
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
- };
5
+ import { configureTokenManager, useTokenManager } from "react-token-manager";
189
6
 
190
7
  // store/useGuardStore.ts
191
8
  import { create } from "zustand";
@@ -236,8 +53,6 @@ function createAuthContext(option) {
236
53
  useEffect(() => {
237
54
  const storedUser = manager.getSingleToken("user");
238
55
  const token = manager.getSingleToken(tokenKey);
239
- const expired = manager.isExpired(token);
240
- console.log("is expired", expired);
241
56
  if (token && !manager.isExpired(token)) {
242
57
  try {
243
58
  setAuth(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.3.11",
3
+ "version": "1.3.13",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -23,5 +23,8 @@
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.4"
26
29
  }
27
30
  }
@@ -56,11 +56,6 @@ export function createAuthContext<UserType extends User = User>(option?: {
56
56
  const storedUser = manager.getSingleToken('user')
57
57
  const token = manager.getSingleToken(tokenKey)
58
58
 
59
- const expired = manager.isExpired(token as any);
60
-
61
- console.log('is expired', expired);
62
-
63
-
64
59
  if (token && !manager.isExpired(token)) {
65
60
  try {
66
61
  setAuth(true)