react-token-manager 1.0.0
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/LICENSE +6 -0
- package/README.md +132 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +1571 -0
- package/dist/index.mjs +1555 -0
- package/package.json +25 -0
- package/src/core.ts +75 -0
- package/src/hook.ts +40 -0
- package/src/index.ts +2 -0
- package/src/useTokenManager.ts +38 -0
- package/tsconfig.json +14 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# React Token Manager
|
|
2
|
+
|
|
3
|
+
A simple React library to manage JWT tokens with support for `localStorage`, `sessionStorage`, and cookies. Features include storing tokens, decoding JWTs, checking expiration, and a convenient React hook.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install react-token-manager
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Global Configuration
|
|
14
|
+
|
|
15
|
+
Set storage type and token key once for your app:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
import { configureTokenManager } from 'react-token-manager'
|
|
19
|
+
|
|
20
|
+
configureTokenManager({
|
|
21
|
+
storage: 'localStorage', // 'localStorage' | 'sessionStorage' | 'cookie'
|
|
22
|
+
tokenKey: 'access_token', // default: 'access_token'
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
## Using the Hook useTokenManager
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
import { useTokenManager } from 'react-token-manager'
|
|
29
|
+
|
|
30
|
+
export default function Dashboard() {
|
|
31
|
+
const { token, setToken, removeToken, isExpired, decode } = useTokenManager()
|
|
32
|
+
|
|
33
|
+
const handleLogin = () => setToken('your.jwt.token.here')
|
|
34
|
+
const handleLogout = () => removeToken()
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div>
|
|
38
|
+
<p>Token: {token}</p>
|
|
39
|
+
<p>Expired? {isExpired() ? 'Yes' : 'No'}</p>
|
|
40
|
+
<p>Decoded Payload: {JSON.stringify(decode())}</p>
|
|
41
|
+
<button onClick={handleLogin}>Login</button>
|
|
42
|
+
<button onClick={handleLogout}>Logout</button>
|
|
43
|
+
</div>
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
You can override storage or tokenKey per hook if needed.
|
|
49
|
+
|
|
50
|
+
## Using TokenManager Class Directly
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
import { TokenManager } from 'react-token-manager'
|
|
54
|
+
|
|
55
|
+
const manager = new TokenManager({ storage: 'cookie', tokenKey: 'access_token' })
|
|
56
|
+
|
|
57
|
+
manager.set('your.jwt.token')
|
|
58
|
+
console.log(manager.get())
|
|
59
|
+
console.log(manager.decode()) // decoded JWT payload
|
|
60
|
+
console.log(manager.isExpired())
|
|
61
|
+
manager.remove()
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API Reference
|
|
65
|
+
|
|
66
|
+
configureTokenManager(options: TokenManagerOptions)
|
|
67
|
+
|
|
68
|
+
Set global options.
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
{
|
|
72
|
+
storage?: 'localStorage' | 'sessionStorage' | 'cookie';
|
|
73
|
+
tokenKey?: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
useTokenManager(options?: TokenManagerOptions)
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
|
|
80
|
+
token: string | null — current token
|
|
81
|
+
|
|
82
|
+
setToken(value: string) — set a new token
|
|
83
|
+
|
|
84
|
+
removeToken() — remove the token
|
|
85
|
+
|
|
86
|
+
isExpired(): boolean — check expiration
|
|
87
|
+
|
|
88
|
+
decode<T>(): T | null — decode JWT payload
|
|
89
|
+
|
|
90
|
+
TokenManager Class Methods
|
|
91
|
+
|
|
92
|
+
set(token: string) — store token
|
|
93
|
+
|
|
94
|
+
get(): string | null — retrieve token
|
|
95
|
+
|
|
96
|
+
remove() — delete token
|
|
97
|
+
|
|
98
|
+
decode<T = unknown>(): T | null — decode JWT payload
|
|
99
|
+
|
|
100
|
+
isExpired(): boolean — check if token is expired
|
|
101
|
+
|
|
102
|
+
getValid(): string | null — get token only if valid
|
|
103
|
+
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Examples
|
|
107
|
+
|
|
108
|
+
Multiple Tokens
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
const { token: accessToken, setToken: setAccess } = useTokenManager({ tokenKey: 'access_token' })
|
|
112
|
+
const { token: refreshToken, setToken: setRefresh } = useTokenManager({ tokenKey: 'refresh_token' })
|
|
113
|
+
|
|
114
|
+
setAccess('abc')
|
|
115
|
+
setRefresh('xyz')
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
##Using Cookies
|
|
119
|
+
```bash
|
|
120
|
+
configureTokenManager({ storage: 'cookie', tokenKey: 'access_token' })
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Checking Expiry Before API Call
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
const manager = useTokenManager()
|
|
127
|
+
|
|
128
|
+
if (!manager.isExpired()) {
|
|
129
|
+
callApi(manager.token)
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type StorageType = 'localStorage' | 'sessionStorage' | 'cookie';
|
|
2
|
+
interface TokenManagerOptions {
|
|
3
|
+
storage?: StorageType;
|
|
4
|
+
tokenKey?: string;
|
|
5
|
+
}
|
|
6
|
+
declare const configureTokenManager: (options: TokenManagerOptions) => void;
|
|
7
|
+
declare class TokenManager {
|
|
8
|
+
private storage;
|
|
9
|
+
private tokenKey;
|
|
10
|
+
constructor(options?: TokenManagerOptions);
|
|
11
|
+
set(token: string): void;
|
|
12
|
+
get(): string | null;
|
|
13
|
+
remove(): void;
|
|
14
|
+
decode<T = unknown>(): T | null;
|
|
15
|
+
isExpired(): boolean;
|
|
16
|
+
getValid(): string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface UseTokenManagerReturn {
|
|
20
|
+
token: string | null;
|
|
21
|
+
setToken: (value: string) => void;
|
|
22
|
+
removeToken: () => void;
|
|
23
|
+
isExpired: () => boolean;
|
|
24
|
+
decode: <T = unknown>() => T | null;
|
|
25
|
+
}
|
|
26
|
+
declare const useTokenManager: (options?: TokenManagerOptions) => UseTokenManagerReturn;
|
|
27
|
+
|
|
28
|
+
export { type StorageType, TokenManager, type TokenManagerOptions, type UseTokenManagerReturn, configureTokenManager, useTokenManager };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type StorageType = 'localStorage' | 'sessionStorage' | 'cookie';
|
|
2
|
+
interface TokenManagerOptions {
|
|
3
|
+
storage?: StorageType;
|
|
4
|
+
tokenKey?: string;
|
|
5
|
+
}
|
|
6
|
+
declare const configureTokenManager: (options: TokenManagerOptions) => void;
|
|
7
|
+
declare class TokenManager {
|
|
8
|
+
private storage;
|
|
9
|
+
private tokenKey;
|
|
10
|
+
constructor(options?: TokenManagerOptions);
|
|
11
|
+
set(token: string): void;
|
|
12
|
+
get(): string | null;
|
|
13
|
+
remove(): void;
|
|
14
|
+
decode<T = unknown>(): T | null;
|
|
15
|
+
isExpired(): boolean;
|
|
16
|
+
getValid(): string | null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface UseTokenManagerReturn {
|
|
20
|
+
token: string | null;
|
|
21
|
+
setToken: (value: string) => void;
|
|
22
|
+
removeToken: () => void;
|
|
23
|
+
isExpired: () => boolean;
|
|
24
|
+
decode: <T = unknown>() => T | null;
|
|
25
|
+
}
|
|
26
|
+
declare const useTokenManager: (options?: TokenManagerOptions) => UseTokenManagerReturn;
|
|
27
|
+
|
|
28
|
+
export { type StorageType, TokenManager, type TokenManagerOptions, type UseTokenManagerReturn, configureTokenManager, useTokenManager };
|