nextauthz 1.3.24 → 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 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
@@ -178,22 +178,29 @@ var RoleGuard = ({
178
178
  children,
179
179
  allowedRoles,
180
180
  redirectTo = "/unauthorized",
181
- fallback = null
181
+ fallback = null,
182
+ roleProp
182
183
  }) => {
183
184
  const router = (0, import_navigation2.useRouter)();
184
- 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]);
185
192
  (0, import_react3.useEffect)(() => {
186
193
  if (!isAuthChecked) return;
187
- if (!role || !allowedRoles.includes(role)) {
194
+ if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
188
195
  router.replace(redirectTo);
189
196
  }
190
- }, [role, isAuthChecked, allowedRoles, redirectTo, router]);
197
+ }, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router]);
191
198
  if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
192
199
  if (!isAuthenticated) {
193
200
  router.replace(redirectTo);
194
201
  return null;
195
202
  }
196
- if (!role || !allowedRoles.includes(role)) return null;
203
+ if (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null;
197
204
  return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
198
205
  };
199
206
  var RoleGuard_default = RoleGuard;
package/dist/index.mjs CHANGED
@@ -151,22 +151,29 @@ var RoleGuard = ({
151
151
  children,
152
152
  allowedRoles,
153
153
  redirectTo = "/unauthorized",
154
- fallback = null
154
+ fallback = null,
155
+ roleProp
155
156
  }) => {
156
157
  const router = useRouter2();
157
- 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]);
158
165
  useEffect3(() => {
159
166
  if (!isAuthChecked) return;
160
- if (!role || !allowedRoles.includes(role)) {
167
+ if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
161
168
  router.replace(redirectTo);
162
169
  }
163
- }, [role, isAuthChecked, allowedRoles, redirectTo, router]);
170
+ }, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router]);
164
171
  if (!isAuthChecked) return /* @__PURE__ */ jsx3(Fragment2, { children: fallback });
165
172
  if (!isAuthenticated) {
166
173
  router.replace(redirectTo);
167
174
  return null;
168
175
  }
169
- if (!role || !allowedRoles.includes(role)) return null;
176
+ if (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null;
170
177
  return /* @__PURE__ */ jsx3(Fragment2, { children });
171
178
  };
172
179
  var RoleGuard_default = RoleGuard;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.3.24",
3
+ "version": "1.3.26",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -96,7 +96,7 @@ export function createAuthContext<UserType extends User = User>(option?: {
96
96
  if (userData) setUser(userData)
97
97
  setRole(role ?? null)
98
98
  setAuth(true);
99
- setAuthChecked(true);
99
+ setAuthChecked(true)
100
100
  }
101
101
 
102
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 (!role || !allowedRoles.includes(role)) {
40
+ if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
28
41
  router.replace(redirectTo)
29
42
  }
30
- }, [role, isAuthChecked, allowedRoles, redirectTo, router])
43
+ }, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router])
31
44
 
32
45
  if (!isAuthChecked) return <>{fallback}</>
33
46
 
@@ -35,10 +48,9 @@ const RoleGuard = ({
35
48
  router.replace(redirectTo)
36
49
  return null
37
50
  }
38
-
39
51
 
40
52
  // Block rendering if role is not allowed
41
- if (!role || !allowedRoles.includes(role)) return null
53
+ if (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null
42
54
 
43
55
  return <>{children}</>
44
56
  }