react-token-manager 1.0.3 → 1.0.4
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/README.md +16 -5
- package/package.json +1 -1
- package/src/core.ts +46 -8
- package/src/hook.ts +2 -0
package/README.md
CHANGED
|
@@ -27,7 +27,14 @@ configureTokenManager({
|
|
|
27
27
|
import { useTokenManager } from 'react-token-manager'
|
|
28
28
|
|
|
29
29
|
export default function Dashboard() {
|
|
30
|
-
const {
|
|
30
|
+
const {
|
|
31
|
+
setTokens,
|
|
32
|
+
getTokens,
|
|
33
|
+
removeTokens,
|
|
34
|
+
clearAllTokens,
|
|
35
|
+
decodeToken,
|
|
36
|
+
isExpired
|
|
37
|
+
} = useTokenManager()
|
|
31
38
|
|
|
32
39
|
// Set multiple tokens at once
|
|
33
40
|
setTokens({
|
|
@@ -49,6 +56,9 @@ export default function Dashboard() {
|
|
|
49
56
|
// Remove multiple tokens
|
|
50
57
|
removeTokens(['access_token', 'refresh_token'])
|
|
51
58
|
|
|
59
|
+
// Clear all tracked tokens
|
|
60
|
+
clearAllTokens()
|
|
61
|
+
|
|
52
62
|
return (
|
|
53
63
|
<div>
|
|
54
64
|
<p>Access Token: {tokens.access_token}</p>
|
|
@@ -77,20 +87,21 @@ manager.set({
|
|
|
77
87
|
// Get multiple tokens
|
|
78
88
|
const tokens = manager.get(['access_token', 'refresh_token'])
|
|
79
89
|
console.log(tokens.access_token) // 'abc123'
|
|
80
|
-
console.log(tokens.refresh_token) // 'xyz789'
|
|
81
90
|
|
|
82
91
|
// Remove multiple tokens
|
|
83
92
|
manager.remove(['access_token', 'refresh_token'])
|
|
84
93
|
|
|
85
|
-
//
|
|
94
|
+
// Clear all tracked tokens
|
|
95
|
+
manager.clearAll()
|
|
96
|
+
|
|
97
|
+
// Decode and check expiration
|
|
86
98
|
console.log(manager.decode(tokens.access_token!))
|
|
87
99
|
console.log(manager.isExpired(tokens.access_token!))
|
|
100
|
+
|
|
88
101
|
```
|
|
89
102
|
|
|
90
103
|
## API Reference
|
|
91
104
|
|
|
92
|
-
configureTokenManager(options: TokenManagerOptions)
|
|
93
|
-
|
|
94
105
|
Set global options.
|
|
95
106
|
|
|
96
107
|
```bash
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -20,6 +20,7 @@ export const configureTokenManager = (options: TokenManagerOptions) => {
|
|
|
20
20
|
|
|
21
21
|
export class TokenManager {
|
|
22
22
|
private storage: StorageType
|
|
23
|
+
private trackedKeys: Set<string> = new Set()
|
|
23
24
|
|
|
24
25
|
constructor(options?: TokenManagerOptions) {
|
|
25
26
|
const opts = options || globalOptions
|
|
@@ -29,42 +30,64 @@ export class TokenManager {
|
|
|
29
30
|
/** Set multiple tokens at once */
|
|
30
31
|
set(tokens: Record<string, string>) {
|
|
31
32
|
Object.entries(tokens).forEach(([key, value]) => {
|
|
33
|
+
this.trackedKeys.add(key)
|
|
34
|
+
|
|
32
35
|
if (this.storage === 'localStorage') localStorage.setItem(key, value)
|
|
33
36
|
else if (this.storage === 'sessionStorage') sessionStorage.setItem(key, value)
|
|
34
37
|
else document.cookie = `${key}=${value}; path=/`
|
|
35
38
|
})
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
/** Get
|
|
41
|
+
/** Get token(s) */
|
|
39
42
|
get(keys: string | string[]): Record<string, string | null> {
|
|
40
43
|
const keyArray = Array.isArray(keys) ? keys : [keys]
|
|
41
44
|
const result: Record<string, string | null> = {}
|
|
42
45
|
|
|
43
46
|
keyArray.forEach((key) => {
|
|
44
47
|
let value: string | null = null
|
|
48
|
+
|
|
45
49
|
if (this.storage === 'localStorage') value = localStorage.getItem(key)
|
|
46
50
|
else if (this.storage === 'sessionStorage') value = sessionStorage.getItem(key)
|
|
47
51
|
else {
|
|
48
|
-
const match = document.cookie.match(
|
|
52
|
+
const match = document.cookie.match(
|
|
53
|
+
new RegExp('(^| )' + key + '=([^;]+)')
|
|
54
|
+
)
|
|
49
55
|
value = match ? match[2] : null
|
|
50
56
|
}
|
|
57
|
+
|
|
51
58
|
result[key] = value
|
|
52
59
|
})
|
|
53
60
|
|
|
54
61
|
return result
|
|
55
62
|
}
|
|
56
63
|
|
|
57
|
-
/** Remove
|
|
64
|
+
/** Remove specific tokens */
|
|
58
65
|
remove(keys: string | string[]) {
|
|
59
66
|
const keyArray = Array.isArray(keys) ? keys : [keys]
|
|
67
|
+
|
|
60
68
|
keyArray.forEach((key) => {
|
|
61
69
|
if (this.storage === 'localStorage') localStorage.removeItem(key)
|
|
62
70
|
else if (this.storage === 'sessionStorage') sessionStorage.removeItem(key)
|
|
63
|
-
else
|
|
71
|
+
else
|
|
72
|
+
document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
|
|
73
|
+
|
|
74
|
+
this.trackedKeys.delete(key)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Clear all tracked auth tokens */
|
|
79
|
+
clearAll() {
|
|
80
|
+
this.trackedKeys.forEach((key) => {
|
|
81
|
+
if (this.storage === 'localStorage') localStorage.removeItem(key)
|
|
82
|
+
else if (this.storage === 'sessionStorage') sessionStorage.removeItem(key)
|
|
83
|
+
else
|
|
84
|
+
document.cookie = `${key}=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
|
|
64
85
|
})
|
|
86
|
+
|
|
87
|
+
this.trackedKeys.clear()
|
|
65
88
|
}
|
|
66
89
|
|
|
67
|
-
/** Decode
|
|
90
|
+
/** Decode token */
|
|
68
91
|
decode<T = unknown>(token: string): T | null {
|
|
69
92
|
if (!token) return null
|
|
70
93
|
try {
|
|
@@ -74,10 +97,25 @@ export class TokenManager {
|
|
|
74
97
|
}
|
|
75
98
|
}
|
|
76
99
|
|
|
77
|
-
/** Check if a token is expired
|
|
78
|
-
|
|
79
|
-
|
|
100
|
+
/** Check if a token is expired
|
|
101
|
+
* If no token is provided, defaults to checking 'access_token'
|
|
102
|
+
*/
|
|
103
|
+
isExpired(token?: string): boolean {
|
|
104
|
+
let tokenToCheck = token
|
|
105
|
+
|
|
106
|
+
// If no token passed, try to get access_token
|
|
107
|
+
if (!tokenToCheck) {
|
|
108
|
+
const stored = this.get('access_token')
|
|
109
|
+
tokenToCheck = stored['access_token'] || undefined
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (!tokenToCheck) return true
|
|
113
|
+
|
|
114
|
+
const decoded = this.decode<JwtPayload>(tokenToCheck)
|
|
115
|
+
|
|
80
116
|
if (!decoded || !decoded.exp) return true
|
|
117
|
+
|
|
81
118
|
return Date.now() >= decoded.exp * 1000
|
|
82
119
|
}
|
|
120
|
+
|
|
83
121
|
}
|
package/src/hook.ts
CHANGED
|
@@ -14,6 +14,8 @@ export const useTokenManager = () => {
|
|
|
14
14
|
/** Remove multiple tokens by keys */
|
|
15
15
|
removeTokens: (keys: string | string[]) => manager.remove(keys),
|
|
16
16
|
|
|
17
|
+
clearTokens: () => manager.clearAll(),
|
|
18
|
+
|
|
17
19
|
/** Decode a single token */
|
|
18
20
|
decodeToken: <T = unknown>(token: string) => manager.decode<T>(token),
|
|
19
21
|
|