nextauthz 1.3.26 → 1.3.28

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
@@ -1,12 +1,11 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import * as React from 'react';
3
- import React__default from 'react';
2
+ import React, { ReactNode } from 'react';
4
3
 
5
- type User$1 = {
4
+ type User = {
6
5
  [key: string]: any;
7
6
  };
8
7
 
9
- type AuthContextType<UserType extends User$1 = User$1> = {
8
+ type AuthContextType<UserType extends User = User> = {
10
9
  user: UserType | null;
11
10
  role: string | null;
12
11
  isAuthenticated: boolean;
@@ -16,33 +15,31 @@ type AuthContextType<UserType extends User$1 = User$1> = {
16
15
  setUser: (user: UserType | null) => void;
17
16
  tokenKey: string;
18
17
  };
18
+ declare function createAuthContext<UserType extends User = User>(option?: {
19
+ storage?: 'localStorage' | 'sessionStorage' | 'cookie';
20
+ tokenKey?: string;
21
+ rolePath?: string;
22
+ }): {
23
+ AuthProvider: ({ children }: {
24
+ children: ReactNode;
25
+ }) => react_jsx_runtime.JSX.Element;
26
+ useAuth: () => AuthContextType<UserType>;
27
+ tokenKey: string;
28
+ };
19
29
 
20
30
  type AuthGuardProps = {
21
- children: React__default.ReactNode;
31
+ children: React.ReactNode;
22
32
  redirectTo?: string;
23
- fallback?: React__default.ReactNode;
33
+ fallback?: React.ReactNode;
24
34
  };
25
35
  declare const AuthGuard: ({ children, redirectTo, fallback, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
26
36
 
27
37
  type RoleGuardProps = {
28
- children: React__default.ReactNode;
38
+ children: React.ReactNode;
29
39
  allowedRoles: string[];
30
40
  redirectTo?: string;
31
- fallback?: React__default.ReactNode;
32
- roleProp?: string;
33
- };
34
- declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, roleProp, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
35
-
36
- type User = Record<string, any>;
37
- /**
38
- * Factory to create isolated auth instance
39
- */
40
- declare function createAppAuth(storage: 'localStorage' | 'sessionStorage' | 'cookie', tokenKey?: string): {
41
- AuthProvider: ({ children }: {
42
- children: React.ReactNode;
43
- }) => react_jsx_runtime.JSX.Element;
44
- useAuth: () => AuthContextType<User$1>;
45
- tokenKey: string;
41
+ fallback?: React.ReactNode;
46
42
  };
43
+ declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
47
44
 
48
- export { AuthGuard, RoleGuard, type User, createAppAuth };
45
+ export { AuthGuard, RoleGuard, createAuthContext };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,11 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import * as React from 'react';
3
- import React__default from 'react';
2
+ import React, { ReactNode } from 'react';
4
3
 
5
- type User$1 = {
4
+ type User = {
6
5
  [key: string]: any;
7
6
  };
8
7
 
9
- type AuthContextType<UserType extends User$1 = User$1> = {
8
+ type AuthContextType<UserType extends User = User> = {
10
9
  user: UserType | null;
11
10
  role: string | null;
12
11
  isAuthenticated: boolean;
@@ -16,33 +15,31 @@ type AuthContextType<UserType extends User$1 = User$1> = {
16
15
  setUser: (user: UserType | null) => void;
17
16
  tokenKey: string;
18
17
  };
18
+ declare function createAuthContext<UserType extends User = User>(option?: {
19
+ storage?: 'localStorage' | 'sessionStorage' | 'cookie';
20
+ tokenKey?: string;
21
+ rolePath?: string;
22
+ }): {
23
+ AuthProvider: ({ children }: {
24
+ children: ReactNode;
25
+ }) => react_jsx_runtime.JSX.Element;
26
+ useAuth: () => AuthContextType<UserType>;
27
+ tokenKey: string;
28
+ };
19
29
 
20
30
  type AuthGuardProps = {
21
- children: React__default.ReactNode;
31
+ children: React.ReactNode;
22
32
  redirectTo?: string;
23
- fallback?: React__default.ReactNode;
33
+ fallback?: React.ReactNode;
24
34
  };
25
35
  declare const AuthGuard: ({ children, redirectTo, fallback, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
26
36
 
27
37
  type RoleGuardProps = {
28
- children: React__default.ReactNode;
38
+ children: React.ReactNode;
29
39
  allowedRoles: string[];
30
40
  redirectTo?: string;
31
- fallback?: React__default.ReactNode;
32
- roleProp?: string;
33
- };
34
- declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, roleProp, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
35
-
36
- type User = Record<string, any>;
37
- /**
38
- * Factory to create isolated auth instance
39
- */
40
- declare function createAppAuth(storage: 'localStorage' | 'sessionStorage' | 'cookie', tokenKey?: string): {
41
- AuthProvider: ({ children }: {
42
- children: React.ReactNode;
43
- }) => react_jsx_runtime.JSX.Element;
44
- useAuth: () => AuthContextType<User$1>;
45
- tokenKey: string;
41
+ fallback?: React.ReactNode;
46
42
  };
43
+ declare const RoleGuard: ({ children, allowedRoles, redirectTo, fallback, }: RoleGuardProps) => react_jsx_runtime.JSX.Element | null;
47
44
 
48
- export { AuthGuard, RoleGuard, type User, createAppAuth };
45
+ export { AuthGuard, RoleGuard, createAuthContext };
package/dist/index.js CHANGED
@@ -23,7 +23,7 @@ var index_exports = {};
23
23
  __export(index_exports, {
24
24
  AuthGuard: () => AuthGuard_default,
25
25
  RoleGuard: () => RoleGuard_default,
26
- createAppAuth: () => createAppAuth
26
+ createAuthContext: () => createAuthContext
27
27
  });
28
28
  module.exports = __toCommonJS(index_exports);
29
29
 
@@ -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
- console.log("storage", storage);
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?.role ?? null);
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 ?? null);
114
+ setRole(role2 ?? extractRole(userData));
111
115
  setAuth(true);
112
116
  setAuthChecked(true);
113
117
  };
@@ -178,40 +182,28 @@ 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: 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
+ const { role, isAuthChecked, isAuthenticated } = useAuthStore();
192
189
  (0, import_react3.useEffect)(() => {
193
190
  if (!isAuthChecked) return;
194
- if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
191
+ if (!role || !allowedRoles.includes(role)) {
195
192
  router.replace(redirectTo);
196
193
  }
197
- }, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router]);
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 (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null;
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;
207
-
208
- // src/index.ts
209
- function createAppAuth(storage, tokenKey = "access_token") {
210
- return createAuthContext({ storage, tokenKey });
211
- }
212
204
  // Annotate the CommonJS export names for ESM import in node:
213
205
  0 && (module.exports = {
214
206
  AuthGuard,
215
207
  RoleGuard,
216
- createAppAuth
208
+ createAuthContext
217
209
  });
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
- console.log("storage", storage);
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?.role ?? null);
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 ?? null);
87
+ setRole(role2 ?? extractRole(userData));
84
88
  setAuth(true);
85
89
  setAuthChecked(true);
86
90
  };
@@ -151,39 +155,27 @@ 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: 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
+ const { role, isAuthChecked, isAuthenticated } = useAuthStore();
165
162
  useEffect3(() => {
166
163
  if (!isAuthChecked) return;
167
- if (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
164
+ if (!role || !allowedRoles.includes(role)) {
168
165
  router.replace(redirectTo);
169
166
  }
170
- }, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router]);
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 (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null;
173
+ if (!role || !allowedRoles.includes(role)) return null;
177
174
  return /* @__PURE__ */ jsx3(Fragment2, { children });
178
175
  };
179
176
  var RoleGuard_default = RoleGuard;
180
-
181
- // src/index.ts
182
- function createAppAuth(storage, tokenKey = "access_token") {
183
- return createAuthContext({ storage, tokenKey });
184
- }
185
177
  export {
186
178
  AuthGuard_default as AuthGuard,
187
179
  RoleGuard_default as RoleGuard,
188
- createAppAuth
180
+ createAuthContext
189
181
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.3.26",
3
+ "version": "1.3.28",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -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 as any)?.role ?? null)
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 ?? null)
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: 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])
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 (!roleToCheck || !allowedRoles.includes(roleToCheck)) {
27
+ if (!role || !allowedRoles.includes(role)) {
41
28
  router.replace(redirectTo)
42
29
  }
43
- }, [roleToCheck, isAuthChecked, allowedRoles, redirectTo, router])
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 (!roleToCheck || !allowedRoles.includes(roleToCheck)) return null
40
+ if (!role || !allowedRoles.includes(role)) return null
54
41
 
55
42
  return <>{children}</>
56
43
  }
package/src/index.ts CHANGED
@@ -1,18 +1,7 @@
1
1
  'use client'
2
2
 
3
- import { createAuthContext } from './AuthProvider'
3
+ export { createAuthContext } from './AuthProvider'
4
4
 
5
5
  export { default as AuthGuard } from './AuthGuard'
6
6
  export { default as RoleGuard } from './RoleGuard'
7
7
 
8
- export type User = Record<string, any>
9
-
10
- /**
11
- * Factory to create isolated auth instance
12
- */
13
- export function createAppAuth(
14
- storage: 'localStorage' | 'sessionStorage' | 'cookie',
15
- tokenKey: string = 'access_token' // <-- default token key
16
- ) {
17
- return createAuthContext({ storage, tokenKey })
18
- }