nextauthz 1.1.8 → 1.2.0

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,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import React__default, { ReactNode } from 'react';
3
+ import React__default from 'react';
4
4
 
5
5
  type AuthContextType<UserType> = {
6
6
  user: UserType | null;
@@ -10,18 +10,6 @@ type AuthContextType<UserType> = {
10
10
  loading: boolean;
11
11
  error: Error | null;
12
12
  };
13
- type AuthContextOptions = {
14
- storage?: 'localStorage' | 'sessionStorage' | 'cookie';
15
- };
16
- /**
17
- * Factory to create AuthProvider and typed useAuth hook
18
- */
19
- declare function createAuthContext<UserType>(options?: AuthContextOptions): {
20
- AuthProvider: ({ children }: {
21
- children: ReactNode;
22
- }) => react_jsx_runtime.JSX.Element;
23
- useAuth: () => AuthContextType<UserType>;
24
- };
25
13
 
26
14
  type AuthGuardProps = {
27
15
  children: React__default.ReactNode;
@@ -36,16 +24,18 @@ type RoleGuardProps = {
36
24
  allowedRoles: string[];
37
25
  redirectTo?: string;
38
26
  };
39
- declare const RoleGuard: React__default.FC<RoleGuardProps>;
27
+ declare const RoleGuard: ({ children, allowedRoles, redirectTo, }: RoleGuardProps) => react_jsx_runtime.JSX.Element;
40
28
 
41
- type User = {
42
- [key: string]: any;
43
- };
29
+ type User = any;
44
30
  declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
45
31
  AuthProvider: ({ children }: {
46
32
  children: React.ReactNode;
47
33
  }) => react_jsx_runtime.JSX.Element;
48
- useAuth: () => AuthContextType<User>;
34
+ useAuth: () => AuthContextType<any>;
49
35
  };
36
+ declare const AuthProvider: ({ children }: {
37
+ children: React.ReactNode;
38
+ }) => react_jsx_runtime.JSX.Element;
39
+ declare const useAuth: () => AuthContextType<any>;
50
40
 
51
- export { AuthGuard, RoleGuard, type User, createAppAuth, createAuthContext };
41
+ export { AuthGuard, AuthProvider, RoleGuard, type User, createAppAuth, useAuth };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as React from 'react';
3
- import React__default, { ReactNode } from 'react';
3
+ import React__default from 'react';
4
4
 
5
5
  type AuthContextType<UserType> = {
6
6
  user: UserType | null;
@@ -10,18 +10,6 @@ type AuthContextType<UserType> = {
10
10
  loading: boolean;
11
11
  error: Error | null;
12
12
  };
13
- type AuthContextOptions = {
14
- storage?: 'localStorage' | 'sessionStorage' | 'cookie';
15
- };
16
- /**
17
- * Factory to create AuthProvider and typed useAuth hook
18
- */
19
- declare function createAuthContext<UserType>(options?: AuthContextOptions): {
20
- AuthProvider: ({ children }: {
21
- children: ReactNode;
22
- }) => react_jsx_runtime.JSX.Element;
23
- useAuth: () => AuthContextType<UserType>;
24
- };
25
13
 
26
14
  type AuthGuardProps = {
27
15
  children: React__default.ReactNode;
@@ -36,16 +24,18 @@ type RoleGuardProps = {
36
24
  allowedRoles: string[];
37
25
  redirectTo?: string;
38
26
  };
39
- declare const RoleGuard: React__default.FC<RoleGuardProps>;
27
+ declare const RoleGuard: ({ children, allowedRoles, redirectTo, }: RoleGuardProps) => react_jsx_runtime.JSX.Element;
40
28
 
41
- type User = {
42
- [key: string]: any;
43
- };
29
+ type User = any;
44
30
  declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
45
31
  AuthProvider: ({ children }: {
46
32
  children: React.ReactNode;
47
33
  }) => react_jsx_runtime.JSX.Element;
48
- useAuth: () => AuthContextType<User>;
34
+ useAuth: () => AuthContextType<any>;
49
35
  };
36
+ declare const AuthProvider: ({ children }: {
37
+ children: React.ReactNode;
38
+ }) => react_jsx_runtime.JSX.Element;
39
+ declare const useAuth: () => AuthContextType<any>;
50
40
 
51
- export { AuthGuard, RoleGuard, type User, createAppAuth, createAuthContext };
41
+ export { AuthGuard, AuthProvider, RoleGuard, type User, createAppAuth, useAuth };
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ "use client";
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -21,9 +22,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
22
  var index_exports = {};
22
23
  __export(index_exports, {
23
24
  AuthGuard: () => AuthGuard_default,
25
+ AuthProvider: () => AuthProvider,
24
26
  RoleGuard: () => RoleGuard_default,
25
27
  createAppAuth: () => createAppAuth,
26
- createAuthContext: () => createAuthContext
28
+ useAuth: () => useAuth
27
29
  });
28
30
  module.exports = __toCommonJS(index_exports);
29
31
 
@@ -54,7 +56,7 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
54
56
  var import_jsx_runtime = require("react/jsx-runtime");
55
57
  function createAuthContext(options) {
56
58
  const AuthContext = (0, import_react.createContext)(null);
57
- const AuthProvider = ({ children }) => {
59
+ const AuthProvider2 = ({ children }) => {
58
60
  const storageType = options?.storage || "cookie";
59
61
  (0, import_react.useEffect)(() => {
60
62
  (0, import_react_token_manager.configureTokenManager)({ storage: storageType });
@@ -73,9 +75,7 @@ function createAuthContext(options) {
73
75
  useAuthStore.getState().setError(null);
74
76
  } catch {
75
77
  useAuthStore.getState().resetAuth();
76
- useAuthStore.getState().setError(
77
- new Error("Failed to parse saved user")
78
- );
78
+ useAuthStore.getState().setError(new Error("Failed to parse saved user"));
79
79
  }
80
80
  }
81
81
  setLoading(false);
@@ -113,12 +113,12 @@ function createAuthContext(options) {
113
113
  }
114
114
  );
115
115
  };
116
- const useAuth = () => {
116
+ const useAuth2 = () => {
117
117
  const ctx = (0, import_react.useContext)(AuthContext);
118
118
  if (!ctx) throw new Error("useAuth must be used inside AuthProvider");
119
119
  return ctx;
120
120
  };
121
- return { AuthProvider, useAuth };
121
+ return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
122
122
  }
123
123
 
124
124
  // src/AuthGuard.tsx
@@ -178,18 +178,12 @@ var AuthGuard_default = AuthGuard;
178
178
  // src/RoleGuard.tsx
179
179
  var import_react3 = require("react");
180
180
  var import_navigation2 = require("next/navigation");
181
-
182
- // src/myAuth.ts
183
- var auth = createAppAuth();
184
-
185
- // src/RoleGuard.tsx
186
181
  var import_jsx_runtime3 = require("react/jsx-runtime");
187
182
  var RoleGuard = ({
188
183
  children,
189
184
  allowedRoles,
190
185
  redirectTo = "/unauthorized"
191
186
  }) => {
192
- const { useAuth } = auth;
193
187
  const { user, loading } = useAuth();
194
188
  const router = (0, import_navigation2.useRouter)();
195
189
  const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
@@ -210,10 +204,12 @@ var RoleGuard_default = RoleGuard;
210
204
  function createAppAuth(storage = "cookie") {
211
205
  return createAuthContext({ storage });
212
206
  }
207
+ var { AuthProvider, useAuth } = createAppAuth();
213
208
  // Annotate the CommonJS export names for ESM import in node:
214
209
  0 && (module.exports = {
215
210
  AuthGuard,
211
+ AuthProvider,
216
212
  RoleGuard,
217
213
  createAppAuth,
218
- createAuthContext
214
+ useAuth
219
215
  });
package/dist/index.mjs CHANGED
@@ -1,10 +1,7 @@
1
+ "use client";
2
+
1
3
  // src/AuthProvider.tsx
2
- import {
3
- createContext,
4
- useContext,
5
- useEffect,
6
- useState
7
- } from "react";
4
+ import { createContext, useContext, useEffect, useState } from "react";
8
5
  import { configureTokenManager, useTokenManager } from "react-token-manager";
9
6
 
10
7
  // store/useGuardStore.ts
@@ -30,7 +27,7 @@ var useAuthStore = create((set) => ({
30
27
  import { jsx } from "react/jsx-runtime";
31
28
  function createAuthContext(options) {
32
29
  const AuthContext = createContext(null);
33
- const AuthProvider = ({ children }) => {
30
+ const AuthProvider2 = ({ children }) => {
34
31
  const storageType = options?.storage || "cookie";
35
32
  useEffect(() => {
36
33
  configureTokenManager({ storage: storageType });
@@ -49,9 +46,7 @@ function createAuthContext(options) {
49
46
  useAuthStore.getState().setError(null);
50
47
  } catch {
51
48
  useAuthStore.getState().resetAuth();
52
- useAuthStore.getState().setError(
53
- new Error("Failed to parse saved user")
54
- );
49
+ useAuthStore.getState().setError(new Error("Failed to parse saved user"));
55
50
  }
56
51
  }
57
52
  setLoading(false);
@@ -89,12 +84,12 @@ function createAuthContext(options) {
89
84
  }
90
85
  );
91
86
  };
92
- const useAuth = () => {
87
+ const useAuth2 = () => {
93
88
  const ctx = useContext(AuthContext);
94
89
  if (!ctx) throw new Error("useAuth must be used inside AuthProvider");
95
90
  return ctx;
96
91
  };
97
- return { AuthProvider, useAuth };
92
+ return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
98
93
  }
99
94
 
100
95
  // src/AuthGuard.tsx
@@ -154,18 +149,12 @@ var AuthGuard_default = AuthGuard;
154
149
  // src/RoleGuard.tsx
155
150
  import { useEffect as useEffect3, useState as useState3 } from "react";
156
151
  import { useRouter as useRouter2 } from "next/navigation";
157
-
158
- // src/myAuth.ts
159
- var auth = createAppAuth();
160
-
161
- // src/RoleGuard.tsx
162
152
  import { Fragment as Fragment2, jsx as jsx3 } from "react/jsx-runtime";
163
153
  var RoleGuard = ({
164
154
  children,
165
155
  allowedRoles,
166
156
  redirectTo = "/unauthorized"
167
157
  }) => {
168
- const { useAuth } = auth;
169
158
  const { user, loading } = useAuth();
170
159
  const router = useRouter2();
171
160
  const [isChecking, setIsChecking] = useState3(true);
@@ -186,9 +175,11 @@ var RoleGuard_default = RoleGuard;
186
175
  function createAppAuth(storage = "cookie") {
187
176
  return createAuthContext({ storage });
188
177
  }
178
+ var { AuthProvider, useAuth } = createAppAuth();
189
179
  export {
190
180
  AuthGuard_default as AuthGuard,
181
+ AuthProvider,
191
182
  RoleGuard_default as RoleGuard,
192
183
  createAppAuth,
193
- createAuthContext
184
+ useAuth
194
185
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -1,12 +1,6 @@
1
1
  'use client'
2
2
 
3
- import React, {
4
- createContext,
5
- useContext,
6
- ReactNode,
7
- useEffect,
8
- useState,
9
- } from 'react'
3
+ import React, { createContext, useContext, ReactNode, useEffect, useState } from 'react'
10
4
  import { configureTokenManager, useTokenManager } from 'react-token-manager'
11
5
  import { useAuthStore } from '../store/useGuardStore'
12
6
 
@@ -24,7 +18,7 @@ export type AuthContextOptions = {
24
18
  }
25
19
 
26
20
  /**
27
- * Factory to create AuthProvider and typed useAuth hook
21
+ * Factory to create AuthProvider + typed useAuth hook
28
22
  */
29
23
  export function createAuthContext<UserType>(options?: AuthContextOptions) {
30
24
  const AuthContext = createContext<AuthContextType<UserType> | null>(null)
@@ -32,21 +26,19 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
32
26
  const AuthProvider = ({ children }: { children: ReactNode }) => {
33
27
  const storageType = options?.storage || 'cookie'
34
28
 
35
- // Configure token manager once on mount
29
+ // Configure token manager once
36
30
  useEffect(() => {
37
31
  configureTokenManager({ storage: storageType })
38
32
  }, [storageType])
39
33
 
40
- // ✅ Hooks must be called inside component
41
34
  const manager = useTokenManager()
42
-
43
35
  const [loading, setLoading] = useState(true)
44
36
 
45
37
  const rawUser = useAuthStore((state) => state.user)
46
38
  const error = useAuthStore((state) => state.error)
47
39
  const user = rawUser as UserType | null
48
40
 
49
- // Restore saved user from token manager
41
+ // Restore saved user from token manager on mount
50
42
  useEffect(() => {
51
43
  const savedUser = manager.getSingleToken('user')
52
44
  if (savedUser) {
@@ -56,9 +48,7 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
56
48
  useAuthStore.getState().setError(null)
57
49
  } catch {
58
50
  useAuthStore.getState().resetAuth()
59
- useAuthStore.getState().setError(
60
- new Error('Failed to parse saved user')
61
- )
51
+ useAuthStore.getState().setError(new Error('Failed to parse saved user'))
62
52
  }
63
53
  }
64
54
  setLoading(false)
package/src/RoleGuard.tsx CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import React, { useEffect, useState } from 'react'
4
4
  import { useRouter } from 'next/navigation'
5
- import { auth } from './myAuth'
5
+ import { useAuth } from '.'
6
6
 
7
7
  type RoleGuardProps = {
8
8
  children: React.ReactNode
@@ -10,15 +10,12 @@ type RoleGuardProps = {
10
10
  redirectTo?: string
11
11
  }
12
12
 
13
- const RoleGuard: React.FC<RoleGuardProps> = ({
13
+ const RoleGuard = ({
14
14
  children,
15
15
  allowedRoles,
16
16
  redirectTo = '/unauthorized',
17
- }) => {
18
- // ✅ Destructure useAuth INSIDE the component
19
- const { useAuth } = auth
17
+ }: RoleGuardProps) => {
20
18
  const { user, loading } = useAuth()
21
-
22
19
  const router = useRouter()
23
20
  const [isChecking, setIsChecking] = useState(true)
24
21
 
package/src/index.ts CHANGED
@@ -1,16 +1,18 @@
1
+ 'use client'
2
+
1
3
  import { createAuthContext } from './AuthProvider'
2
4
 
3
- export type User = {
4
- [key: string]: any
5
- }
5
+ export { default as AuthGuard } from './AuthGuard'
6
+ export { default as RoleGuard } from './RoleGuard'
6
7
 
7
- // Export factory instead of fixed instance
8
+ export type User = any
9
+
10
+ // Factory function for app
8
11
  export function createAppAuth(
9
12
  storage: 'localStorage' | 'sessionStorage' | 'cookie' = 'cookie'
10
13
  ) {
11
14
  return createAuthContext<User>({ storage })
12
15
  }
13
16
 
14
- export { createAuthContext } from './AuthProvider'
15
- export { default as AuthGuard } from './AuthGuard'
16
- export { default as RoleGuard } from './RoleGuard'
17
+ // Default instance (optional)
18
+ export const { AuthProvider, useAuth } = createAppAuth()
package/src/myAuth.ts DELETED
@@ -1,4 +0,0 @@
1
- // myAuth.ts
2
- 'use client'
3
- import { createAppAuth } from '.'
4
- export const auth = createAppAuth() // { AuthProvider, useAuth }