nextauthz 1.3.32 → 1.3.34
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 +0 -2
- package/dist/index.mjs +0 -2
- package/package.json +1 -1
- package/readme.md +154 -72
- package/src/AuthProvider.tsx +0 -2
package/dist/index.js
CHANGED
|
@@ -92,8 +92,6 @@ function createAuthContext(option) {
|
|
|
92
92
|
setAuth(true);
|
|
93
93
|
if (storedUser) {
|
|
94
94
|
const parsedUser = JSON.parse(storedUser);
|
|
95
|
-
console.log("parsedUser", parsedUser);
|
|
96
|
-
console.log("extractRole", extractRole(parsedUser));
|
|
97
95
|
setUser(parsedUser);
|
|
98
96
|
setRole(extractRole(parsedUser));
|
|
99
97
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -65,8 +65,6 @@ function createAuthContext(option) {
|
|
|
65
65
|
setAuth(true);
|
|
66
66
|
if (storedUser) {
|
|
67
67
|
const parsedUser = JSON.parse(storedUser);
|
|
68
|
-
console.log("parsedUser", parsedUser);
|
|
69
|
-
console.log("extractRole", extractRole(parsedUser));
|
|
70
68
|
setUser(parsedUser);
|
|
71
69
|
setRole(extractRole(parsedUser));
|
|
72
70
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -2,106 +2,153 @@
|
|
|
2
2
|
|
|
3
3
|
## Overview
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
** NextAuthZ provides:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
** Global auth state
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
** Token persistence
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
** Typed AuthProvider & useAuth
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
** Route protection (AuthGuard)
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
** Role-based protection (RoleGuard)
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
** Configurable storage system
|
|
18
|
+
|
|
19
|
+
** Configurable role extraction (rolePath)
|
|
20
|
+
|
|
21
|
+
Works perfectly with Next.js App Router.
|
|
18
22
|
|
|
19
23
|
## Installation
|
|
20
24
|
|
|
21
25
|
npm install nextauthz
|
|
22
26
|
|
|
27
|
+
## Setup
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
1️⃣ Create Your Auth Instance
|
|
25
30
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Wrap your application with AuthProvider to provide global auth state (user, loading, error) and helper functions (login, logout, setUser).
|
|
31
|
+
Because NextAuthZ uses a factory pattern, you must create your own auth instance.
|
|
29
32
|
|
|
30
33
|
```bash
|
|
31
|
-
|
|
34
|
+
// src/lib/auth.ts
|
|
35
|
+
import { createAuthContext } from 'nextauthz'
|
|
36
|
+
|
|
37
|
+
export const {
|
|
38
|
+
AuthProvider,
|
|
39
|
+
useAuth,
|
|
40
|
+
} = createAuthContext({
|
|
41
|
+
storage: 'cookie', // 'localStorage' | 'sessionStorage' | 'cookie'
|
|
42
|
+
tokenKey: 'access_token', // optional (default: 'access_token')
|
|
43
|
+
rolePath: 'account_type', // optional (default: 'role')
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Available Options
|
|
32
48
|
|
|
33
|
-
|
|
34
|
-
|
|
49
|
+
| Option | Type | Default | Description |
|
|
50
|
+
| ---------- | ------------------------------------------------ | ---------------- | ------------------------------------- |
|
|
51
|
+
| `storage` | `'localStorage' \| 'sessionStorage' \| 'cookie'` | `'cookie'` | Where tokens are stored |
|
|
52
|
+
| `tokenKey` | `string` | `'access_token'` | Key used for primary token |
|
|
53
|
+
| `rolePath` | `string` | `'role'` | Path to extract role from user object |
|
|
35
54
|
|
|
36
55
|
|
|
37
|
-
// Options: 'localStorage' | 'sessionStorage' | 'cookie'
|
|
38
|
-
// Defaults to 'cookie' if no storage is passed
|
|
39
56
|
|
|
40
|
-
|
|
57
|
+
|
|
58
|
+
## 2️⃣ Wrap Your App
|
|
59
|
+
|
|
60
|
+
Wrap your application with AuthProvider to provide global auth state (user, loading, error) and helper functions (login, logout, setUser).
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
// app/layout.tsx
|
|
64
|
+
|
|
65
|
+
import { AuthProvider } from '@/lib/auth'
|
|
66
|
+
|
|
67
|
+
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
|
41
68
|
return <AuthProvider>{children}</AuthProvider>
|
|
42
69
|
}
|
|
43
|
-
|
|
44
|
-
export default App
|
|
45
70
|
```
|
|
46
71
|
|
|
47
|
-
## Hook
|
|
72
|
+
## useAuth Hook
|
|
48
73
|
|
|
49
74
|
```bash
|
|
50
|
-
import { useAuth } from '
|
|
51
|
-
|
|
52
|
-
const {
|
|
75
|
+
import { useAuth } from '@/lib/auth'
|
|
76
|
+
|
|
77
|
+
const {
|
|
78
|
+
user,
|
|
79
|
+
role,
|
|
80
|
+
isAuthenticated,
|
|
81
|
+
isAuthChecked,
|
|
82
|
+
login,
|
|
83
|
+
logout,
|
|
84
|
+
setUser,
|
|
85
|
+
} = useAuth()
|
|
53
86
|
```
|
|
54
87
|
|
|
55
88
|
Available Properties
|
|
56
89
|
|
|
57
|
-
| Name | Type | Description
|
|
58
|
-
| ----------------- | ------------------------------------ |
|
|
59
|
-
| `user` | `User \| null` | Current authenticated user
|
|
60
|
-
| `role` | `string \| null` |
|
|
61
|
-
| `isAuthenticated` | `boolean` | True if
|
|
62
|
-
| `isAuthChecked` | `boolean` | True
|
|
63
|
-
| `login` | `(tokens, userData?, role?) => void` |
|
|
64
|
-
| `logout` | `() => void` | Clears
|
|
65
|
-
| `setUser` | `(user
|
|
90
|
+
| Name | Type | Description |
|
|
91
|
+
| ----------------- | ------------------------------------ | ---------------------------------------- |
|
|
92
|
+
| `user` | `User \| null` | Current authenticated user |
|
|
93
|
+
| `role` | `string \| null` | Extracted user role |
|
|
94
|
+
| `isAuthenticated` | `boolean` | True if logged in |
|
|
95
|
+
| `isAuthChecked` | `boolean` | True when auth hydration is complete |
|
|
96
|
+
| `login` | `(tokens, userData?, role?) => void` | Save tokens + optionally set user + role |
|
|
97
|
+
| `logout` | `() => void` | Clears tokens and resets auth |
|
|
98
|
+
| `setUser` | `(user \| null) => void` | Manually update user |
|
|
66
99
|
|
|
67
|
-
|
|
68
|
-
Example: Logging in
|
|
100
|
+
Example: Logging In
|
|
69
101
|
|
|
70
102
|
```bash
|
|
71
|
-
const
|
|
103
|
+
const { login } = useAuth()
|
|
104
|
+
|
|
105
|
+
const handleLogin = async () => {
|
|
72
106
|
const tokens = {
|
|
73
107
|
access_token: 'abc123',
|
|
74
108
|
refresh_token: 'xyz789',
|
|
75
109
|
}
|
|
76
110
|
|
|
77
111
|
const user = {
|
|
78
|
-
id: 1,
|
|
112
|
+
id: '1',
|
|
79
113
|
name: 'John',
|
|
80
|
-
|
|
114
|
+
account_type: 'admin',
|
|
81
115
|
}
|
|
82
116
|
|
|
83
|
-
login(tokens, user
|
|
117
|
+
login(tokens, user)
|
|
84
118
|
}
|
|
119
|
+
```
|
|
85
120
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
121
|
+
If rolePath: 'account_type' is configured, role is automatically extracted.
|
|
122
|
+
|
|
123
|
+
You can also override it manually:
|
|
124
|
+
|
|
125
|
+
``` bash
|
|
126
|
+
login(tokens, user, 'admin')
|
|
127
|
+
```
|
|
128
|
+
## Logging Out
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
const { logout } = useAuth()
|
|
132
|
+
|
|
133
|
+
const handleLogin = async () => {
|
|
134
|
+
logout()
|
|
135
|
+
router.replace('/authentication/login');
|
|
136
|
+
}
|
|
90
137
|
```
|
|
91
138
|
|
|
92
139
|
## AuthGuard
|
|
93
140
|
|
|
94
141
|
Purpose
|
|
95
142
|
|
|
96
|
-
Protect
|
|
143
|
+
Protect pages so only authenticated users can access them.
|
|
97
144
|
|
|
98
145
|
Props
|
|
99
146
|
|
|
100
|
-
| Name | Type | Default | Description
|
|
101
|
-
| ------------ | ----------- | -------- |
|
|
102
|
-
| `children` | `ReactNode` | required |
|
|
103
|
-
| `redirectTo` | `string` | `/login` |
|
|
104
|
-
| `fallback` | `ReactNode` | `null` | Optional loading
|
|
147
|
+
| Name | Type | Default | Description |
|
|
148
|
+
| ------------ | ----------- | -------- | ---------------------------------- |
|
|
149
|
+
| `children` | `ReactNode` | required | Content to render if authenticated |
|
|
150
|
+
| `redirectTo` | `string` | `/login` | Redirect if not authenticated |
|
|
151
|
+
| `fallback` | `ReactNode` | `null` | Optional loading UI |
|
|
105
152
|
|
|
106
153
|
## Usage Example
|
|
107
154
|
|
|
@@ -118,36 +165,34 @@ function DashboardPage() {
|
|
|
118
165
|
```
|
|
119
166
|
Behavior:
|
|
120
167
|
|
|
121
|
-
**
|
|
168
|
+
** Waits for isAuthChecked
|
|
169
|
+
** Redirects to redirectTo value if not authenticated
|
|
170
|
+
** Renders children if authenticated
|
|
122
171
|
|
|
123
172
|
|
|
124
173
|
## RoleGuard
|
|
125
174
|
|
|
126
|
-
|
|
175
|
+
Restrict access based on role.
|
|
127
176
|
|
|
128
|
-
|
|
177
|
+
Props
|
|
129
178
|
|
|
130
|
-
| Name | Type | Default | Description
|
|
131
|
-
| -------------- | ----------- | --------------- |
|
|
132
|
-
| `children` | `ReactNode` | required |
|
|
133
|
-
| `allowedRoles` | `string[]` | required |
|
|
134
|
-
| `redirectTo` | `string` | `/unauthorized` | Redirect
|
|
135
|
-
| `fallback` | `ReactNode` | `null` | Optional loading
|
|
179
|
+
| Name | Type | Default | Description |
|
|
180
|
+
| -------------- | ----------- | --------------- | ---------------------------- |
|
|
181
|
+
| `children` | `ReactNode` | required | Content to render |
|
|
182
|
+
| `allowedRoles` | `string[]` | required | Allowed roles |
|
|
183
|
+
| `redirectTo` | `string` | `/unauthorized` | Redirect if role not allowed |
|
|
184
|
+
| `fallback` | `ReactNode` | `null` | Optional loading UI |
|
|
136
185
|
|
|
137
186
|
|
|
138
187
|
Usage Example
|
|
139
188
|
|
|
140
189
|
```bash
|
|
141
|
-
import RoleGuard from 'nextauthz'
|
|
142
|
-
import { useAuth } from 'nextauthz'
|
|
143
|
-
|
|
144
|
-
function AdminPage() {
|
|
145
|
-
const { user } = useAuth()
|
|
190
|
+
import { RoleGuard } from 'nextauthz'
|
|
146
191
|
|
|
192
|
+
export default function AdminPage() {
|
|
147
193
|
return (
|
|
148
|
-
<RoleGuard allowedRoles={['admin']} fallback={<p>Checking
|
|
194
|
+
<RoleGuard allowedRoles={['admin']} fallback={<p>Checking role...</p>}>
|
|
149
195
|
<h1>Admin Dashboard</h1>
|
|
150
|
-
<p>Welcome, {user?.name}</p>
|
|
151
196
|
</RoleGuard>
|
|
152
197
|
)
|
|
153
198
|
}
|
|
@@ -155,19 +200,52 @@ function AdminPage() {
|
|
|
155
200
|
|
|
156
201
|
Behavior:
|
|
157
202
|
|
|
158
|
-
**
|
|
159
|
-
**
|
|
203
|
+
** Waits for isAuthChecked
|
|
204
|
+
** Redirects if role not in allowedRoles
|
|
205
|
+
** Blocks rendering if unauthorized
|
|
160
206
|
|
|
161
207
|
|
|
162
208
|
## Storage Options
|
|
163
209
|
|
|
164
210
|
You can configure token storage:
|
|
165
211
|
|
|
166
|
-
| Storage Type |
|
|
167
|
-
| ---------------- |
|
|
168
|
-
| `localStorage` | Persists until
|
|
169
|
-
| `sessionStorage` | Clears on tab close
|
|
170
|
-
| `cookie` | Cookie-based
|
|
212
|
+
| Storage Type | Behavior |
|
|
213
|
+
| ---------------- | ---------------------- |
|
|
214
|
+
| `localStorage` | Persists until cleared |
|
|
215
|
+
| `sessionStorage` | Clears on tab close |
|
|
216
|
+
| `cookie` | Cookie-based (default) |
|
|
217
|
+
|
|
218
|
+
## Role Path Examples
|
|
219
|
+
Given this user:
|
|
220
|
+
|
|
221
|
+
```bash
|
|
222
|
+
{
|
|
223
|
+
id: "1",
|
|
224
|
+
email: "user@email.com",
|
|
225
|
+
account_type: "artist"
|
|
226
|
+
}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Use:
|
|
230
|
+
|
|
231
|
+
```bash
|
|
232
|
+
rolePath: 'account_type'
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
Nested example:
|
|
236
|
+
```bash
|
|
237
|
+
{
|
|
238
|
+
profile: {
|
|
239
|
+
role: 'admin'
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Use:
|
|
245
|
+
|
|
246
|
+
```bash
|
|
247
|
+
rolePath: 'profile.role'
|
|
248
|
+
```
|
|
171
249
|
|
|
172
250
|
|
|
173
251
|
## Why NextAuthZ?
|
|
@@ -184,4 +262,8 @@ You can configure token storage:
|
|
|
184
262
|
|
|
185
263
|
** Lightweight
|
|
186
264
|
|
|
187
|
-
** No opinionated backend
|
|
265
|
+
** No opinionated backend
|
|
266
|
+
|
|
267
|
+
** Backend agnostic
|
|
268
|
+
|
|
269
|
+
** Factory-based (multiple auth instances supported)
|
package/src/AuthProvider.tsx
CHANGED
|
@@ -75,8 +75,6 @@ export function createAuthContext<UserType extends User = User>(option?: {
|
|
|
75
75
|
|
|
76
76
|
if (storedUser) {
|
|
77
77
|
const parsedUser = JSON.parse(storedUser) as UserType
|
|
78
|
-
console.log('parsedUser', parsedUser);
|
|
79
|
-
console.log('extractRole', extractRole(parsedUser));
|
|
80
78
|
setUser(parsedUser)
|
|
81
79
|
setRole(extractRole(parsedUser))
|
|
82
80
|
}
|