nextauthz 1.3.25 → 1.3.26
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 +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +14 -10
- package/dist/index.mjs +14 -10
- package/package.json +1 -1
- package/src/AuthProvider.tsx +2 -5
- package/src/RoleGuard.tsx +17 -4
package/dist/index.d.mts
CHANGED
|
@@ -29,8 +29,9 @@ type RoleGuardProps = {
|
|
|
29
29
|
allowedRoles: string[];
|
|
30
30
|
redirectTo?: string;
|
|
31
31
|
fallback?: React__default.ReactNode;
|
|
32
|
+
roleProp?: string;
|
|
32
33
|
};
|
|
33
|
-
declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
34
|
+
declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, roleProp, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
34
35
|
|
|
35
36
|
type User = Record<string, any>;
|
|
36
37
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -29,8 +29,9 @@ type RoleGuardProps = {
|
|
|
29
29
|
allowedRoles: string[];
|
|
30
30
|
redirectTo?: string;
|
|
31
31
|
fallback?: React__default.ReactNode;
|
|
32
|
+
roleProp?: string;
|
|
32
33
|
};
|
|
33
|
-
declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
34
|
+
declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, roleProp, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
34
35
|
|
|
35
36
|
type User = Record<string, any>;
|
|
36
37
|
/**
|
package/dist/index.js
CHANGED
|
@@ -82,16 +82,14 @@ function createAuthContext(option) {
|
|
|
82
82
|
} = useAuthStore();
|
|
83
83
|
(0, import_react.useEffect)(() => {
|
|
84
84
|
const storedUser = manager.getSingleToken("user");
|
|
85
|
-
const role2 = manager.getSingleToken("role");
|
|
86
85
|
const token = manager.getSingleToken(tokenKey);
|
|
87
86
|
if (token && !manager.isExpired(token)) {
|
|
88
87
|
try {
|
|
89
88
|
setAuth(true);
|
|
90
89
|
if (storedUser) {
|
|
91
90
|
const parsedUser = JSON.parse(storedUser);
|
|
92
|
-
const parsedRole = JSON.parse(role2);
|
|
93
91
|
setUser(parsedUser);
|
|
94
|
-
setRole(
|
|
92
|
+
setRole(parsedUser?.role ?? null);
|
|
95
93
|
}
|
|
96
94
|
} catch {
|
|
97
95
|
resetAuth();
|
|
@@ -106,8 +104,7 @@ function createAuthContext(option) {
|
|
|
106
104
|
manager.setTokens({
|
|
107
105
|
...tokens,
|
|
108
106
|
[tokenKey]: tokenValue,
|
|
109
|
-
user: JSON.stringify(userData ?? null)
|
|
110
|
-
role: JSON.stringify(user?.account_type ?? null)
|
|
107
|
+
user: JSON.stringify(userData ?? null)
|
|
111
108
|
});
|
|
112
109
|
if (userData) setUser(userData);
|
|
113
110
|
setRole(role2 ?? null);
|
|
@@ -181,22 +178,29 @@ var RoleGuard = ({
|
|
|
181
178
|
children,
|
|
182
179
|
allowedRoles,
|
|
183
180
|
redirectTo = "/unauthorized",
|
|
184
|
-
fallback = null
|
|
181
|
+
fallback = null,
|
|
182
|
+
roleProp
|
|
185
183
|
}) => {
|
|
186
184
|
const router = (0, import_navigation2.useRouter)();
|
|
187
|
-
const { role, isAuthChecked, isAuthenticated } = useAuthStore();
|
|
185
|
+
const { role: storeRole, setRole, isAuthChecked, isAuthenticated, user } = useAuthStore();
|
|
186
|
+
const roleToCheck = roleProp ?? storeRole ?? (user && (user.account_type ?? (user.role ?? null)));
|
|
187
|
+
(0, import_react3.useEffect)(() => {
|
|
188
|
+
if (!storeRole && roleToCheck) {
|
|
189
|
+
setRole(roleToCheck);
|
|
190
|
+
}
|
|
191
|
+
}, [roleToCheck, storeRole, setRole]);
|
|
188
192
|
(0, import_react3.useEffect)(() => {
|
|
189
193
|
if (!isAuthChecked) return;
|
|
190
|
-
if (!
|
|
194
|
+
if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
|
|
191
195
|
router.replace(redirectTo);
|
|
192
196
|
}
|
|
193
|
-
}, [
|
|
197
|
+
}, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router]);
|
|
194
198
|
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
|
|
195
199
|
if (!isAuthenticated) {
|
|
196
200
|
router.replace(redirectTo);
|
|
197
201
|
return null;
|
|
198
202
|
}
|
|
199
|
-
if (!
|
|
203
|
+
if (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null;
|
|
200
204
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
201
205
|
};
|
|
202
206
|
var RoleGuard_default = RoleGuard;
|
package/dist/index.mjs
CHANGED
|
@@ -55,16 +55,14 @@ function createAuthContext(option) {
|
|
|
55
55
|
} = useAuthStore();
|
|
56
56
|
useEffect(() => {
|
|
57
57
|
const storedUser = manager.getSingleToken("user");
|
|
58
|
-
const role2 = manager.getSingleToken("role");
|
|
59
58
|
const token = manager.getSingleToken(tokenKey);
|
|
60
59
|
if (token && !manager.isExpired(token)) {
|
|
61
60
|
try {
|
|
62
61
|
setAuth(true);
|
|
63
62
|
if (storedUser) {
|
|
64
63
|
const parsedUser = JSON.parse(storedUser);
|
|
65
|
-
const parsedRole = JSON.parse(role2);
|
|
66
64
|
setUser(parsedUser);
|
|
67
|
-
setRole(
|
|
65
|
+
setRole(parsedUser?.role ?? null);
|
|
68
66
|
}
|
|
69
67
|
} catch {
|
|
70
68
|
resetAuth();
|
|
@@ -79,8 +77,7 @@ function createAuthContext(option) {
|
|
|
79
77
|
manager.setTokens({
|
|
80
78
|
...tokens,
|
|
81
79
|
[tokenKey]: tokenValue,
|
|
82
|
-
user: JSON.stringify(userData ?? null)
|
|
83
|
-
role: JSON.stringify(user?.account_type ?? null)
|
|
80
|
+
user: JSON.stringify(userData ?? null)
|
|
84
81
|
});
|
|
85
82
|
if (userData) setUser(userData);
|
|
86
83
|
setRole(role2 ?? null);
|
|
@@ -154,22 +151,29 @@ var RoleGuard = ({
|
|
|
154
151
|
children,
|
|
155
152
|
allowedRoles,
|
|
156
153
|
redirectTo = "/unauthorized",
|
|
157
|
-
fallback = null
|
|
154
|
+
fallback = null,
|
|
155
|
+
roleProp
|
|
158
156
|
}) => {
|
|
159
157
|
const router = useRouter2();
|
|
160
|
-
const { role, isAuthChecked, isAuthenticated } = useAuthStore();
|
|
158
|
+
const { role: storeRole, setRole, isAuthChecked, isAuthenticated, user } = useAuthStore();
|
|
159
|
+
const roleToCheck = roleProp ?? storeRole ?? (user && (user.account_type ?? (user.role ?? null)));
|
|
160
|
+
useEffect3(() => {
|
|
161
|
+
if (!storeRole && roleToCheck) {
|
|
162
|
+
setRole(roleToCheck);
|
|
163
|
+
}
|
|
164
|
+
}, [roleToCheck, storeRole, setRole]);
|
|
161
165
|
useEffect3(() => {
|
|
162
166
|
if (!isAuthChecked) return;
|
|
163
|
-
if (!
|
|
167
|
+
if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
|
|
164
168
|
router.replace(redirectTo);
|
|
165
169
|
}
|
|
166
|
-
}, [
|
|
170
|
+
}, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router]);
|
|
167
171
|
if (!isAuthChecked) return /* @__PURE__ */ jsx3(Fragment2, { children: fallback });
|
|
168
172
|
if (!isAuthenticated) {
|
|
169
173
|
router.replace(redirectTo);
|
|
170
174
|
return null;
|
|
171
175
|
}
|
|
172
|
-
if (!
|
|
176
|
+
if (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null;
|
|
173
177
|
return /* @__PURE__ */ jsx3(Fragment2, { children });
|
|
174
178
|
};
|
|
175
179
|
var RoleGuard_default = RoleGuard;
|
package/package.json
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -58,7 +58,6 @@ export function createAuthContext<UserType extends User = User>(option?: {
|
|
|
58
58
|
|
|
59
59
|
useEffect(() => {
|
|
60
60
|
const storedUser = manager.getSingleToken('user')
|
|
61
|
-
const role = manager.getSingleToken('role')
|
|
62
61
|
const token = manager.getSingleToken(tokenKey)
|
|
63
62
|
|
|
64
63
|
if (token && !manager.isExpired(token)) {
|
|
@@ -67,9 +66,8 @@ export function createAuthContext<UserType extends User = User>(option?: {
|
|
|
67
66
|
|
|
68
67
|
if (storedUser) {
|
|
69
68
|
const parsedUser = JSON.parse(storedUser) as UserType
|
|
70
|
-
const parsedRole = JSON.parse(role as string)
|
|
71
69
|
setUser(parsedUser)
|
|
72
|
-
setRole((
|
|
70
|
+
setRole((parsedUser as any)?.role ?? null)
|
|
73
71
|
}
|
|
74
72
|
} catch {
|
|
75
73
|
resetAuth()
|
|
@@ -93,13 +91,12 @@ export function createAuthContext<UserType extends User = User>(option?: {
|
|
|
93
91
|
...tokens,
|
|
94
92
|
[tokenKey]: tokenValue,
|
|
95
93
|
user: JSON.stringify(userData ?? null),
|
|
96
|
-
role: JSON.stringify(user?.account_type ?? null)
|
|
97
94
|
})
|
|
98
95
|
|
|
99
96
|
if (userData) setUser(userData)
|
|
100
97
|
setRole(role ?? null)
|
|
101
98
|
setAuth(true);
|
|
102
|
-
setAuthChecked(true)
|
|
99
|
+
setAuthChecked(true)
|
|
103
100
|
}
|
|
104
101
|
|
|
105
102
|
/* ---------------------------------- */
|
package/src/RoleGuard.tsx
CHANGED
|
@@ -9,6 +9,7 @@ type RoleGuardProps = {
|
|
|
9
9
|
allowedRoles: string[]
|
|
10
10
|
redirectTo?: string
|
|
11
11
|
fallback?: React.ReactNode
|
|
12
|
+
roleProp?: string // pass role explicitly if store role is empty
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
const RoleGuard = ({
|
|
@@ -16,18 +17,30 @@ const RoleGuard = ({
|
|
|
16
17
|
allowedRoles,
|
|
17
18
|
redirectTo = '/unauthorized',
|
|
18
19
|
fallback = null,
|
|
20
|
+
roleProp,
|
|
19
21
|
}: RoleGuardProps) => {
|
|
20
22
|
const router = useRouter()
|
|
21
|
-
const { role, isAuthChecked, isAuthenticated } = useAuthStore()
|
|
23
|
+
const { role: storeRole, setRole, isAuthChecked, isAuthenticated, user } = useAuthStore()
|
|
24
|
+
|
|
25
|
+
// Determine which role to check
|
|
26
|
+
const roleToCheck =
|
|
27
|
+
roleProp ?? storeRole ?? (user && (user.account_type ?? (user.role ?? null)))
|
|
28
|
+
|
|
29
|
+
// Update store role if missing
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!storeRole && roleToCheck) {
|
|
32
|
+
setRole(roleToCheck)
|
|
33
|
+
}
|
|
34
|
+
}, [roleToCheck, storeRole, setRole])
|
|
22
35
|
|
|
23
36
|
useEffect(() => {
|
|
24
37
|
if (!isAuthChecked) return
|
|
25
38
|
|
|
26
39
|
// If role not allowed, redirect
|
|
27
|
-
if (!
|
|
40
|
+
if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
|
|
28
41
|
router.replace(redirectTo)
|
|
29
42
|
}
|
|
30
|
-
}, [
|
|
43
|
+
}, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router])
|
|
31
44
|
|
|
32
45
|
if (!isAuthChecked) return <>{fallback}</>
|
|
33
46
|
|
|
@@ -37,7 +50,7 @@ const RoleGuard = ({
|
|
|
37
50
|
}
|
|
38
51
|
|
|
39
52
|
// Block rendering if role is not allowed
|
|
40
|
-
if (!
|
|
53
|
+
if (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null
|
|
41
54
|
|
|
42
55
|
return <>{children}</>
|
|
43
56
|
}
|