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