nextauthz 1.0.5 → 1.0.7

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
@@ -14,7 +14,7 @@ type AuthContextOptions = {
14
14
  storage?: 'localStorage' | 'sessionStorage' | 'cookie';
15
15
  };
16
16
  /**
17
- * Factory to create typed AuthProvider and useAuth hook
17
+ * Factory to create AuthProvider and typed useAuth hook
18
18
  */
19
19
  declare function createAuthContext<UserType>(options?: AuthContextOptions): {
20
20
  AuthProvider: ({ children }: {
package/dist/index.d.ts CHANGED
@@ -14,7 +14,7 @@ type AuthContextOptions = {
14
14
  storage?: 'localStorage' | 'sessionStorage' | 'cookie';
15
15
  };
16
16
  /**
17
- * Factory to create typed AuthProvider and useAuth hook
17
+ * Factory to create AuthProvider and typed useAuth hook
18
18
  */
19
19
  declare function createAuthContext<UserType>(options?: AuthContextOptions): {
20
20
  AuthProvider: ({ children }: {
package/dist/index.js CHANGED
@@ -1791,7 +1791,7 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
1791
1791
  var import_jsx_runtime = __toESM(require_jsx_runtime());
1792
1792
  function createAuthContext(options) {
1793
1793
  const AuthContext = (0, import_react.createContext)(null);
1794
- const AuthProvider2 = ({ children }) => {
1794
+ const AuthProvider = ({ children }) => {
1795
1795
  const storageType = options?.storage || "cookie";
1796
1796
  (0, import_react.useEffect)(() => {
1797
1797
  (0, import_react_token_manager.configureTokenManager)({ storage: storageType });
@@ -1852,12 +1852,10 @@ function createAuthContext(options) {
1852
1852
  };
1853
1853
  const useAuth2 = () => {
1854
1854
  const ctx = (0, import_react.useContext)(AuthContext);
1855
- if (!ctx) {
1856
- throw new Error("useAuth must be used inside AuthProvider");
1857
- }
1855
+ if (!ctx) throw new Error("useAuth must be used inside AuthProvider");
1858
1856
  return ctx;
1859
1857
  };
1860
- return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
1858
+ return { AuthProvider, useAuth: useAuth2 };
1861
1859
  }
1862
1860
 
1863
1861
  // src/AuthGuard.tsx
@@ -1919,10 +1917,11 @@ var import_react3 = __toESM(require_react());
1919
1917
  var import_navigation2 = require("next/navigation");
1920
1918
 
1921
1919
  // src/myAuth.ts
1922
- var { AuthProvider, useAuth } = createAppAuth();
1920
+ var auth = createAppAuth();
1923
1921
 
1924
1922
  // src/RoleGuard.tsx
1925
1923
  var import_jsx_runtime3 = __toESM(require_jsx_runtime());
1924
+ var { useAuth } = auth;
1926
1925
  var RoleGuard = ({
1927
1926
  children,
1928
1927
  allowedRoles,
package/dist/index.mjs CHANGED
@@ -1775,7 +1775,7 @@ var useAuthStore = create((set) => ({
1775
1775
  var import_jsx_runtime = __toESM(require_jsx_runtime());
1776
1776
  function createAuthContext(options) {
1777
1777
  const AuthContext = (0, import_react.createContext)(null);
1778
- const AuthProvider2 = ({ children }) => {
1778
+ const AuthProvider = ({ children }) => {
1779
1779
  const storageType = options?.storage || "cookie";
1780
1780
  (0, import_react.useEffect)(() => {
1781
1781
  configureTokenManager({ storage: storageType });
@@ -1836,12 +1836,10 @@ function createAuthContext(options) {
1836
1836
  };
1837
1837
  const useAuth2 = () => {
1838
1838
  const ctx = (0, import_react.useContext)(AuthContext);
1839
- if (!ctx) {
1840
- throw new Error("useAuth must be used inside AuthProvider");
1841
- }
1839
+ if (!ctx) throw new Error("useAuth must be used inside AuthProvider");
1842
1840
  return ctx;
1843
1841
  };
1844
- return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
1842
+ return { AuthProvider, useAuth: useAuth2 };
1845
1843
  }
1846
1844
 
1847
1845
  // src/AuthGuard.tsx
@@ -1903,10 +1901,11 @@ var import_react3 = __toESM(require_react());
1903
1901
  import { useRouter as useRouter2 } from "next/navigation";
1904
1902
 
1905
1903
  // src/myAuth.ts
1906
- var { AuthProvider, useAuth } = createAppAuth();
1904
+ var auth = createAppAuth();
1907
1905
 
1908
1906
  // src/RoleGuard.tsx
1909
1907
  var import_jsx_runtime3 = __toESM(require_jsx_runtime());
1908
+ var { useAuth } = auth;
1910
1909
  var RoleGuard = ({
1911
1910
  children,
1912
1911
  allowedRoles,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -4,8 +4,8 @@ import React, {
4
4
  createContext,
5
5
  useContext,
6
6
  ReactNode,
7
- useState,
8
7
  useEffect,
8
+ useState,
9
9
  } from 'react'
10
10
  import { configureTokenManager, useTokenManager } from 'react-token-manager'
11
11
  import { useAuthStore } from '../store/useGuardStore'
@@ -24,7 +24,7 @@ export type AuthContextOptions = {
24
24
  }
25
25
 
26
26
  /**
27
- * Factory to create typed AuthProvider and useAuth hook
27
+ * Factory to create AuthProvider and typed useAuth hook
28
28
  */
29
29
  export function createAuthContext<UserType>(options?: AuthContextOptions) {
30
30
  const AuthContext = createContext<AuthContextType<UserType> | null>(null)
@@ -32,21 +32,21 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
32
32
  const AuthProvider = ({ children }: { children: ReactNode }) => {
33
33
  const storageType = options?.storage || 'cookie'
34
34
 
35
- // Configure token manager on mount
35
+ // Configure token manager once on mount
36
36
  useEffect(() => {
37
37
  configureTokenManager({ storage: storageType })
38
38
  }, [storageType])
39
39
 
40
- const manager = useTokenManager() // inside component
40
+ // Hooks must be called inside component
41
+ const manager = useTokenManager()
41
42
 
42
43
  const [loading, setLoading] = useState(true)
43
44
 
44
45
  const rawUser = useAuthStore((state) => state.user)
45
46
  const error = useAuthStore((state) => state.error)
46
-
47
47
  const user = rawUser as UserType | null
48
48
 
49
- // Restore user on mount
49
+ // Restore saved user from token manager
50
50
  useEffect(() => {
51
51
  const savedUser = manager.getSingleToken('user')
52
52
  if (savedUser) {
@@ -103,9 +103,7 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
103
103
 
104
104
  const useAuth = (): AuthContextType<UserType> => {
105
105
  const ctx = useContext(AuthContext)
106
- if (!ctx) {
107
- throw new Error('useAuth must be used inside AuthProvider')
108
- }
106
+ if (!ctx) throw new Error('useAuth must be used inside AuthProvider')
109
107
  return ctx
110
108
  }
111
109
 
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 { useAuth } from './myAuth'
5
+ import { auth } from './myAuth'
6
6
 
7
7
  type RoleGuardProps = {
8
8
  children: React.ReactNode
@@ -10,6 +10,8 @@ type RoleGuardProps = {
10
10
  redirectTo?: string
11
11
  }
12
12
 
13
+ const { useAuth } = auth
14
+
13
15
  const RoleGuard = ({
14
16
  children,
15
17
  allowedRoles,
package/src/myAuth.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { createAppAuth } from "."
1
+ // myAuth.ts
2
+ 'use client'
2
3
 
4
+ import { createAppAuth } from '.'
3
5
 
4
6
  export type User = any
5
7
 
6
- // Create typed AuthProvider and useAuth hook
7
- export const { AuthProvider, useAuth } = createAppAuth()
8
+ // Export the factory
9
+ export const auth = createAppAuth() // { AuthProvider, useAuth }