react-token-manager 1.1.4 → 1.1.6
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.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +37 -19
- package/dist/index.mjs +37 -19
- package/package.json +1 -1
- package/src/core.ts +39 -22
package/dist/index.d.mts
CHANGED
|
@@ -13,12 +13,12 @@ declare class TokenManager {
|
|
|
13
13
|
get(keys: string | string[]): Record<string, string | null>;
|
|
14
14
|
/** Get a single token by key */
|
|
15
15
|
getOne(key: string): string | null;
|
|
16
|
-
/** Get all tracked
|
|
16
|
+
/** Get all tokens in storage (tracked or all) */
|
|
17
17
|
getAll(): Record<string, string | null>;
|
|
18
18
|
/** Remove specific tokens */
|
|
19
19
|
remove(keys: string | string[]): void;
|
|
20
|
-
/** Clear all tracked
|
|
21
|
-
clear(): void;
|
|
20
|
+
/** Clear all tokens, optionally only tracked keys */
|
|
21
|
+
clear(clearAll?: boolean): void;
|
|
22
22
|
/** Decode JWT token safely */
|
|
23
23
|
decode<T = unknown>(token: string): T | null;
|
|
24
24
|
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
package/dist/index.d.ts
CHANGED
|
@@ -13,12 +13,12 @@ declare class TokenManager {
|
|
|
13
13
|
get(keys: string | string[]): Record<string, string | null>;
|
|
14
14
|
/** Get a single token by key */
|
|
15
15
|
getOne(key: string): string | null;
|
|
16
|
-
/** Get all tracked
|
|
16
|
+
/** Get all tokens in storage (tracked or all) */
|
|
17
17
|
getAll(): Record<string, string | null>;
|
|
18
18
|
/** Remove specific tokens */
|
|
19
19
|
remove(keys: string | string[]): void;
|
|
20
|
-
/** Clear all tracked
|
|
21
|
-
clear(): void;
|
|
20
|
+
/** Clear all tokens, optionally only tracked keys */
|
|
21
|
+
clear(clearAll?: boolean): void;
|
|
22
22
|
/** Decode JWT token safely */
|
|
23
23
|
decode<T = unknown>(token: string): T | null;
|
|
24
24
|
/** Check if token is expired. Defaults to 'access_token' if no token is passed */
|
package/dist/index.js
CHANGED
|
@@ -66,12 +66,26 @@ var TokenManager = class {
|
|
|
66
66
|
const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
|
|
67
67
|
return match ? match[2] : null;
|
|
68
68
|
}
|
|
69
|
-
/** Get all tracked
|
|
69
|
+
/** Get all tokens in storage (tracked or all) */
|
|
70
70
|
getAll() {
|
|
71
71
|
const result = {};
|
|
72
|
-
this.
|
|
73
|
-
|
|
74
|
-
|
|
72
|
+
if (this.storage === "localStorage") {
|
|
73
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
74
|
+
const key = localStorage.key(i);
|
|
75
|
+
if (key) result[key] = localStorage.getItem(key);
|
|
76
|
+
}
|
|
77
|
+
} else if (this.storage === "sessionStorage") {
|
|
78
|
+
for (let i = 0; i < sessionStorage.length; i++) {
|
|
79
|
+
const key = sessionStorage.key(i);
|
|
80
|
+
if (key) result[key] = sessionStorage.getItem(key);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
const cookies = document.cookie.split(";");
|
|
84
|
+
cookies.forEach((cookie) => {
|
|
85
|
+
const [key, value] = cookie.split("=");
|
|
86
|
+
if (key && value) result[key.trim()] = value;
|
|
87
|
+
});
|
|
88
|
+
}
|
|
75
89
|
return result;
|
|
76
90
|
}
|
|
77
91
|
/** Remove specific tokens */
|
|
@@ -85,14 +99,23 @@ var TokenManager = class {
|
|
|
85
99
|
this.trackedKeys.delete(key);
|
|
86
100
|
});
|
|
87
101
|
}
|
|
88
|
-
/** Clear all tracked
|
|
89
|
-
clear() {
|
|
90
|
-
this.
|
|
91
|
-
if (
|
|
92
|
-
else
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
102
|
+
/** Clear all tokens, optionally only tracked keys */
|
|
103
|
+
clear(clearAll = false) {
|
|
104
|
+
if (this.storage === "localStorage") {
|
|
105
|
+
if (clearAll) localStorage.clear();
|
|
106
|
+
else this.trackedKeys.forEach((key) => localStorage.removeItem(key));
|
|
107
|
+
} else if (this.storage === "sessionStorage") {
|
|
108
|
+
if (clearAll) sessionStorage.clear();
|
|
109
|
+
else this.trackedKeys.forEach((key) => sessionStorage.removeItem(key));
|
|
110
|
+
} else {
|
|
111
|
+
const cookies = document.cookie.split(";");
|
|
112
|
+
cookies.forEach((cookie) => {
|
|
113
|
+
const [key] = cookie.split("=");
|
|
114
|
+
if (key && (clearAll || this.trackedKeys.has(key.trim()))) {
|
|
115
|
+
document.cookie = `${key.trim()}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
96
119
|
this.trackedKeys.clear();
|
|
97
120
|
}
|
|
98
121
|
/** Decode JWT token safely */
|
|
@@ -100,10 +123,8 @@ var TokenManager = class {
|
|
|
100
123
|
if (!token) return null;
|
|
101
124
|
try {
|
|
102
125
|
const cleanToken = token.trim().replace(/^"|"$/g, "");
|
|
103
|
-
console.log("cleanToken", cleanToken);
|
|
104
126
|
return (0, import_jwt_decode.jwtDecode)(cleanToken);
|
|
105
|
-
} catch
|
|
106
|
-
console.error("JWT decode error:", err, token);
|
|
127
|
+
} catch {
|
|
107
128
|
return null;
|
|
108
129
|
}
|
|
109
130
|
}
|
|
@@ -113,10 +134,7 @@ var TokenManager = class {
|
|
|
113
134
|
if (!tokenToCheck) return true;
|
|
114
135
|
const decoded = this.decode(tokenToCheck);
|
|
115
136
|
if (!decoded || !decoded.exp) return true;
|
|
116
|
-
|
|
117
|
-
const now = Date.now();
|
|
118
|
-
const expTime = decoded.exp * 1e3;
|
|
119
|
-
return now >= expTime;
|
|
137
|
+
return Date.now() >= decoded.exp * 1e3;
|
|
120
138
|
}
|
|
121
139
|
};
|
|
122
140
|
|
package/dist/index.mjs
CHANGED
|
@@ -38,12 +38,26 @@ var TokenManager = class {
|
|
|
38
38
|
const match = document.cookie.match(new RegExp("(^| )" + key + "=([^;]+)"));
|
|
39
39
|
return match ? match[2] : null;
|
|
40
40
|
}
|
|
41
|
-
/** Get all tracked
|
|
41
|
+
/** Get all tokens in storage (tracked or all) */
|
|
42
42
|
getAll() {
|
|
43
43
|
const result = {};
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
|
|
44
|
+
if (this.storage === "localStorage") {
|
|
45
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
46
|
+
const key = localStorage.key(i);
|
|
47
|
+
if (key) result[key] = localStorage.getItem(key);
|
|
48
|
+
}
|
|
49
|
+
} else if (this.storage === "sessionStorage") {
|
|
50
|
+
for (let i = 0; i < sessionStorage.length; i++) {
|
|
51
|
+
const key = sessionStorage.key(i);
|
|
52
|
+
if (key) result[key] = sessionStorage.getItem(key);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
const cookies = document.cookie.split(";");
|
|
56
|
+
cookies.forEach((cookie) => {
|
|
57
|
+
const [key, value] = cookie.split("=");
|
|
58
|
+
if (key && value) result[key.trim()] = value;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
47
61
|
return result;
|
|
48
62
|
}
|
|
49
63
|
/** Remove specific tokens */
|
|
@@ -57,14 +71,23 @@ var TokenManager = class {
|
|
|
57
71
|
this.trackedKeys.delete(key);
|
|
58
72
|
});
|
|
59
73
|
}
|
|
60
|
-
/** Clear all tracked
|
|
61
|
-
clear() {
|
|
62
|
-
this.
|
|
63
|
-
if (
|
|
64
|
-
else
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
/** Clear all tokens, optionally only tracked keys */
|
|
75
|
+
clear(clearAll = false) {
|
|
76
|
+
if (this.storage === "localStorage") {
|
|
77
|
+
if (clearAll) localStorage.clear();
|
|
78
|
+
else this.trackedKeys.forEach((key) => localStorage.removeItem(key));
|
|
79
|
+
} else if (this.storage === "sessionStorage") {
|
|
80
|
+
if (clearAll) sessionStorage.clear();
|
|
81
|
+
else this.trackedKeys.forEach((key) => sessionStorage.removeItem(key));
|
|
82
|
+
} else {
|
|
83
|
+
const cookies = document.cookie.split(";");
|
|
84
|
+
cookies.forEach((cookie) => {
|
|
85
|
+
const [key] = cookie.split("=");
|
|
86
|
+
if (key && (clearAll || this.trackedKeys.has(key.trim()))) {
|
|
87
|
+
document.cookie = `${key.trim()}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
68
91
|
this.trackedKeys.clear();
|
|
69
92
|
}
|
|
70
93
|
/** Decode JWT token safely */
|
|
@@ -72,10 +95,8 @@ var TokenManager = class {
|
|
|
72
95
|
if (!token) return null;
|
|
73
96
|
try {
|
|
74
97
|
const cleanToken = token.trim().replace(/^"|"$/g, "");
|
|
75
|
-
console.log("cleanToken", cleanToken);
|
|
76
98
|
return jwtDecode(cleanToken);
|
|
77
|
-
} catch
|
|
78
|
-
console.error("JWT decode error:", err, token);
|
|
99
|
+
} catch {
|
|
79
100
|
return null;
|
|
80
101
|
}
|
|
81
102
|
}
|
|
@@ -85,10 +106,7 @@ var TokenManager = class {
|
|
|
85
106
|
if (!tokenToCheck) return true;
|
|
86
107
|
const decoded = this.decode(tokenToCheck);
|
|
87
108
|
if (!decoded || !decoded.exp) return true;
|
|
88
|
-
|
|
89
|
-
const now = Date.now();
|
|
90
|
-
const expTime = decoded.exp * 1e3;
|
|
91
|
-
return now >= expTime;
|
|
109
|
+
return Date.now() >= decoded.exp * 1e3;
|
|
92
110
|
}
|
|
93
111
|
};
|
|
94
112
|
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -53,12 +53,28 @@ export class TokenManager {
|
|
|
53
53
|
return match ? match[2] : null
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
/** Get all tracked
|
|
56
|
+
/** Get all tokens in storage (tracked or all) */
|
|
57
57
|
getAll(): Record<string, string | null> {
|
|
58
58
|
const result: Record<string, string | null> = {}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
|
|
60
|
+
if (this.storage === 'localStorage') {
|
|
61
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
62
|
+
const key = localStorage.key(i)
|
|
63
|
+
if (key) result[key] = localStorage.getItem(key)
|
|
64
|
+
}
|
|
65
|
+
} else if (this.storage === 'sessionStorage') {
|
|
66
|
+
for (let i = 0; i < sessionStorage.length; i++) {
|
|
67
|
+
const key = sessionStorage.key(i)
|
|
68
|
+
if (key) result[key] = sessionStorage.getItem(key)
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
const cookies = document.cookie.split(';')
|
|
72
|
+
cookies.forEach((cookie) => {
|
|
73
|
+
const [key, value] = cookie.split('=')
|
|
74
|
+
if (key && value) result[key.trim()] = value
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
62
78
|
return result
|
|
63
79
|
}
|
|
64
80
|
|
|
@@ -74,14 +90,23 @@ export class TokenManager {
|
|
|
74
90
|
})
|
|
75
91
|
}
|
|
76
92
|
|
|
77
|
-
/** Clear all tracked
|
|
78
|
-
clear() {
|
|
79
|
-
this.
|
|
80
|
-
if (
|
|
81
|
-
else
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
93
|
+
/** Clear all tokens, optionally only tracked keys */
|
|
94
|
+
clear(clearAll: boolean = false) {
|
|
95
|
+
if (this.storage === 'localStorage') {
|
|
96
|
+
if (clearAll) localStorage.clear()
|
|
97
|
+
else this.trackedKeys.forEach((key) => localStorage.removeItem(key))
|
|
98
|
+
} else if (this.storage === 'sessionStorage') {
|
|
99
|
+
if (clearAll) sessionStorage.clear()
|
|
100
|
+
else this.trackedKeys.forEach((key) => sessionStorage.removeItem(key))
|
|
101
|
+
} else {
|
|
102
|
+
const cookies = document.cookie.split(';')
|
|
103
|
+
cookies.forEach((cookie) => {
|
|
104
|
+
const [key] = cookie.split('=')
|
|
105
|
+
if (key && (clearAll || this.trackedKeys.has(key.trim()))) {
|
|
106
|
+
document.cookie = `${key.trim()}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
}
|
|
85
110
|
this.trackedKeys.clear()
|
|
86
111
|
}
|
|
87
112
|
|
|
@@ -90,11 +115,8 @@ export class TokenManager {
|
|
|
90
115
|
if (!token) return null
|
|
91
116
|
try {
|
|
92
117
|
const cleanToken = token.trim().replace(/^"|"$/g, '') // Remove quotes
|
|
93
|
-
console.log('cleanToken', cleanToken);
|
|
94
|
-
|
|
95
118
|
return jwtDecode<T>(cleanToken)
|
|
96
|
-
} catch
|
|
97
|
-
console.error('JWT decode error:', err, token)
|
|
119
|
+
} catch {
|
|
98
120
|
return null
|
|
99
121
|
}
|
|
100
122
|
}
|
|
@@ -107,11 +129,6 @@ export class TokenManager {
|
|
|
107
129
|
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
108
130
|
if (!decoded || !decoded.exp) return true
|
|
109
131
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
const now = Date.now()
|
|
114
|
-
const expTime = decoded.exp * 1000
|
|
115
|
-
return now >= expTime
|
|
132
|
+
return Date.now() >= decoded.exp * 1000
|
|
116
133
|
}
|
|
117
134
|
}
|