nextauthz 1.1.1 → 1.1.2

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,20 +1,15 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import * as React from 'react';
3
- import React__default, { ReactNode } from 'react';
2
+ import React, { ReactNode } from 'react';
4
3
 
5
4
  type AuthContextType<UserType> = {
6
- login: (tokens: Record<string, string>, user: UserType) => void;
5
+ user: UserType | null;
6
+ login: (userData: UserType) => void;
7
7
  logout: () => void;
8
- setUser: (user: UserType) => void;
8
+ setUser: (userData: UserType) => void;
9
9
  loading: boolean;
10
+ error: Error | null;
10
11
  };
11
- type AuthContextOptions = {
12
- storage?: 'localStorage' | 'sessionStorage' | 'cookie';
13
- };
14
- /**
15
- * Factory to create AuthProvider and typed useAuth hook
16
- */
17
- declare function createAuthContext<UserType>(options?: AuthContextOptions): {
12
+ declare function createAuthContext<UserType>(): {
18
13
  AuthProvider: ({ children }: {
19
14
  children: ReactNode;
20
15
  }) => react_jsx_runtime.JSX.Element;
@@ -22,28 +17,15 @@ declare function createAuthContext<UserType>(options?: AuthContextOptions): {
22
17
  };
23
18
 
24
19
  type AuthGuardProps = {
25
- children: React__default.ReactNode;
20
+ children: React.ReactNode;
26
21
  redirectTo?: string;
27
22
  tokenKey?: string;
28
23
  refreshToken?: () => Promise<string | null>;
29
24
  };
30
25
  declare const AuthGuard: ({ children, redirectTo, tokenKey, refreshToken, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
31
26
 
32
- type RoleGuardProps = {
33
- children: React__default.ReactNode;
34
- allowedRoles: string[];
35
- redirectTo?: string;
36
- };
37
- declare const RoleGuard: React__default.FC<RoleGuardProps>;
38
-
39
27
  type User = {
40
28
  [key: string]: any;
41
29
  };
42
- declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
43
- AuthProvider: ({ children }: {
44
- children: React.ReactNode;
45
- }) => react_jsx_runtime.JSX.Element;
46
- useAuth: () => AuthContextType<User>;
47
- };
48
30
 
49
- export { AuthGuard, RoleGuard, type User, createAppAuth, createAuthContext };
31
+ export { AuthGuard, type User, createAuthContext };
package/dist/index.d.ts CHANGED
@@ -1,20 +1,15 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import * as React from 'react';
3
- import React__default, { ReactNode } from 'react';
2
+ import React, { ReactNode } from 'react';
4
3
 
5
4
  type AuthContextType<UserType> = {
6
- login: (tokens: Record<string, string>, user: UserType) => void;
5
+ user: UserType | null;
6
+ login: (userData: UserType) => void;
7
7
  logout: () => void;
8
- setUser: (user: UserType) => void;
8
+ setUser: (userData: UserType) => void;
9
9
  loading: boolean;
10
+ error: Error | null;
10
11
  };
11
- type AuthContextOptions = {
12
- storage?: 'localStorage' | 'sessionStorage' | 'cookie';
13
- };
14
- /**
15
- * Factory to create AuthProvider and typed useAuth hook
16
- */
17
- declare function createAuthContext<UserType>(options?: AuthContextOptions): {
12
+ declare function createAuthContext<UserType>(): {
18
13
  AuthProvider: ({ children }: {
19
14
  children: ReactNode;
20
15
  }) => react_jsx_runtime.JSX.Element;
@@ -22,28 +17,15 @@ declare function createAuthContext<UserType>(options?: AuthContextOptions): {
22
17
  };
23
18
 
24
19
  type AuthGuardProps = {
25
- children: React__default.ReactNode;
20
+ children: React.ReactNode;
26
21
  redirectTo?: string;
27
22
  tokenKey?: string;
28
23
  refreshToken?: () => Promise<string | null>;
29
24
  };
30
25
  declare const AuthGuard: ({ children, redirectTo, tokenKey, refreshToken, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
31
26
 
32
- type RoleGuardProps = {
33
- children: React__default.ReactNode;
34
- allowedRoles: string[];
35
- redirectTo?: string;
36
- };
37
- declare const RoleGuard: React__default.FC<RoleGuardProps>;
38
-
39
27
  type User = {
40
28
  [key: string]: any;
41
29
  };
42
- declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
43
- AuthProvider: ({ children }: {
44
- children: React.ReactNode;
45
- }) => react_jsx_runtime.JSX.Element;
46
- useAuth: () => AuthContextType<User>;
47
- };
48
30
 
49
- export { AuthGuard, RoleGuard, type User, createAppAuth, createAuthContext };
31
+ export { AuthGuard, type User, createAuthContext };
package/dist/index.js CHANGED
@@ -1699,18 +1699,18 @@ var require_react_jsx_runtime_development = __commonJS({
1699
1699
  function isValidElement(object) {
1700
1700
  return "object" === typeof object && null !== object && object.$$typeof === REACT_ELEMENT_TYPE;
1701
1701
  }
1702
- var React4 = require_react(), REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for("react.profiler"), REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for("react.memo"), REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = /* @__PURE__ */ Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for("react.client.reference"), ReactSharedInternals = React4.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function() {
1702
+ var React3 = require_react(), REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for("react.profiler"), REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for("react.memo"), REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = /* @__PURE__ */ Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for("react.client.reference"), ReactSharedInternals = React3.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function() {
1703
1703
  return null;
1704
1704
  };
1705
- React4 = {
1705
+ React3 = {
1706
1706
  react_stack_bottom_frame: function(callStackForError) {
1707
1707
  return callStackForError();
1708
1708
  }
1709
1709
  };
1710
1710
  var specialPropKeyWarningShown;
1711
1711
  var didWarnAboutElementRef = {};
1712
- var unknownOwnerDebugStack = React4.react_stack_bottom_frame.bind(
1713
- React4,
1712
+ var unknownOwnerDebugStack = React3.react_stack_bottom_frame.bind(
1713
+ React3,
1714
1714
  UnknownOwner
1715
1715
  )();
1716
1716
  var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));
@@ -1758,8 +1758,6 @@ var require_jsx_runtime = __commonJS({
1758
1758
  var index_exports = {};
1759
1759
  __export(index_exports, {
1760
1760
  AuthGuard: () => AuthGuard_default,
1761
- RoleGuard: () => RoleGuard_default,
1762
- createAppAuth: () => createAppAuth,
1763
1761
  createAuthContext: () => createAuthContext
1764
1762
  });
1765
1763
  module.exports = __toCommonJS(index_exports);
@@ -1767,30 +1765,28 @@ module.exports = __toCommonJS(index_exports);
1767
1765
  // src/AuthProvider.tsx
1768
1766
  var import_react = __toESM(require_react());
1769
1767
  var import_jsx_runtime = __toESM(require_jsx_runtime());
1770
- function createAuthContext(options) {
1768
+ function createAuthContext() {
1771
1769
  const AuthContext = (0, import_react.createContext)(null);
1772
1770
  const AuthProvider = ({ children }) => {
1773
- const [loading, setLoading] = (0, import_react.useState)(true);
1774
- const setUser = (userData) => {
1775
- };
1776
- const login = (tokens, userData) => {
1771
+ const [user, setUser] = (0, import_react.useState)(null);
1772
+ const [loading, setLoading] = (0, import_react.useState)(false);
1773
+ const [error, setError] = (0, import_react.useState)(null);
1774
+ const login = (userData) => {
1775
+ setLoading(true);
1777
1776
  try {
1778
1777
  setUser(userData);
1778
+ setError(null);
1779
1779
  } catch (err) {
1780
+ setError(err instanceof Error ? err : new Error(String(err)));
1781
+ } finally {
1782
+ setLoading(false);
1780
1783
  }
1781
1784
  };
1782
1785
  const logout = () => {
1783
- try {
1784
- } catch (err) {
1785
- }
1786
+ setUser(null);
1787
+ setError(null);
1786
1788
  };
1787
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1788
- AuthContext.Provider,
1789
- {
1790
- value: { login, logout, setUser, loading },
1791
- children
1792
- }
1793
- );
1789
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: { user, login, logout, setUser, loading, error }, children });
1794
1790
  };
1795
1791
  const useAuth = () => {
1796
1792
  const ctx = (0, import_react.useContext)(AuthContext);
@@ -1874,42 +1870,9 @@ var AuthGuard = ({
1874
1870
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
1875
1871
  };
1876
1872
  var AuthGuard_default = AuthGuard;
1877
-
1878
- // src/RoleGuard.tsx
1879
- var import_react3 = __toESM(require_react());
1880
- var import_navigation2 = require("next/navigation");
1881
-
1882
- // src/myAuth.ts
1883
- var auth = createAppAuth();
1884
-
1885
- // src/RoleGuard.tsx
1886
- var import_jsx_runtime3 = __toESM(require_jsx_runtime());
1887
- var RoleGuard = ({
1888
- children,
1889
- allowedRoles,
1890
- redirectTo = "/unauthorized"
1891
- }) => {
1892
- const { useAuth } = auth;
1893
- const { loading } = useAuth();
1894
- const router = (0, import_navigation2.useRouter)();
1895
- const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
1896
- (0, import_react3.useEffect)(() => {
1897
- setIsChecking(false);
1898
- }, [allowedRoles, redirectTo, router]);
1899
- if (loading || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
1900
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
1901
- };
1902
- var RoleGuard_default = RoleGuard;
1903
-
1904
- // src/index.ts
1905
- function createAppAuth(storage = "cookie") {
1906
- return createAuthContext({ storage });
1907
- }
1908
1873
  // Annotate the CommonJS export names for ESM import in node:
1909
1874
  0 && (module.exports = {
1910
1875
  AuthGuard,
1911
- RoleGuard,
1912
- createAppAuth,
1913
1876
  createAuthContext
1914
1877
  });
1915
1878
  /*! Bundled license information:
package/dist/index.mjs CHANGED
@@ -1693,18 +1693,18 @@ var require_react_jsx_runtime_development = __commonJS({
1693
1693
  function isValidElement(object) {
1694
1694
  return "object" === typeof object && null !== object && object.$$typeof === REACT_ELEMENT_TYPE;
1695
1695
  }
1696
- var React4 = require_react(), REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for("react.profiler"), REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for("react.memo"), REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = /* @__PURE__ */ Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for("react.client.reference"), ReactSharedInternals = React4.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function() {
1696
+ var React3 = require_react(), REACT_ELEMENT_TYPE = /* @__PURE__ */ Symbol.for("react.transitional.element"), REACT_PORTAL_TYPE = /* @__PURE__ */ Symbol.for("react.portal"), REACT_FRAGMENT_TYPE = /* @__PURE__ */ Symbol.for("react.fragment"), REACT_STRICT_MODE_TYPE = /* @__PURE__ */ Symbol.for("react.strict_mode"), REACT_PROFILER_TYPE = /* @__PURE__ */ Symbol.for("react.profiler"), REACT_CONSUMER_TYPE = /* @__PURE__ */ Symbol.for("react.consumer"), REACT_CONTEXT_TYPE = /* @__PURE__ */ Symbol.for("react.context"), REACT_FORWARD_REF_TYPE = /* @__PURE__ */ Symbol.for("react.forward_ref"), REACT_SUSPENSE_TYPE = /* @__PURE__ */ Symbol.for("react.suspense"), REACT_SUSPENSE_LIST_TYPE = /* @__PURE__ */ Symbol.for("react.suspense_list"), REACT_MEMO_TYPE = /* @__PURE__ */ Symbol.for("react.memo"), REACT_LAZY_TYPE = /* @__PURE__ */ Symbol.for("react.lazy"), REACT_ACTIVITY_TYPE = /* @__PURE__ */ Symbol.for("react.activity"), REACT_CLIENT_REFERENCE = /* @__PURE__ */ Symbol.for("react.client.reference"), ReactSharedInternals = React3.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, hasOwnProperty = Object.prototype.hasOwnProperty, isArrayImpl = Array.isArray, createTask = console.createTask ? console.createTask : function() {
1697
1697
  return null;
1698
1698
  };
1699
- React4 = {
1699
+ React3 = {
1700
1700
  react_stack_bottom_frame: function(callStackForError) {
1701
1701
  return callStackForError();
1702
1702
  }
1703
1703
  };
1704
1704
  var specialPropKeyWarningShown;
1705
1705
  var didWarnAboutElementRef = {};
1706
- var unknownOwnerDebugStack = React4.react_stack_bottom_frame.bind(
1707
- React4,
1706
+ var unknownOwnerDebugStack = React3.react_stack_bottom_frame.bind(
1707
+ React3,
1708
1708
  UnknownOwner
1709
1709
  )();
1710
1710
  var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));
@@ -1751,30 +1751,28 @@ var require_jsx_runtime = __commonJS({
1751
1751
  // src/AuthProvider.tsx
1752
1752
  var import_react = __toESM(require_react());
1753
1753
  var import_jsx_runtime = __toESM(require_jsx_runtime());
1754
- function createAuthContext(options) {
1754
+ function createAuthContext() {
1755
1755
  const AuthContext = (0, import_react.createContext)(null);
1756
1756
  const AuthProvider = ({ children }) => {
1757
- const [loading, setLoading] = (0, import_react.useState)(true);
1758
- const setUser = (userData) => {
1759
- };
1760
- const login = (tokens, userData) => {
1757
+ const [user, setUser] = (0, import_react.useState)(null);
1758
+ const [loading, setLoading] = (0, import_react.useState)(false);
1759
+ const [error, setError] = (0, import_react.useState)(null);
1760
+ const login = (userData) => {
1761
+ setLoading(true);
1761
1762
  try {
1762
1763
  setUser(userData);
1764
+ setError(null);
1763
1765
  } catch (err) {
1766
+ setError(err instanceof Error ? err : new Error(String(err)));
1767
+ } finally {
1768
+ setLoading(false);
1764
1769
  }
1765
1770
  };
1766
1771
  const logout = () => {
1767
- try {
1768
- } catch (err) {
1769
- }
1772
+ setUser(null);
1773
+ setError(null);
1770
1774
  };
1771
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1772
- AuthContext.Provider,
1773
- {
1774
- value: { login, logout, setUser, loading },
1775
- children
1776
- }
1777
- );
1775
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: { user, login, logout, setUser, loading, error }, children });
1778
1776
  };
1779
1777
  const useAuth = () => {
1780
1778
  const ctx = (0, import_react.useContext)(AuthContext);
@@ -1858,41 +1856,8 @@ var AuthGuard = ({
1858
1856
  return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
1859
1857
  };
1860
1858
  var AuthGuard_default = AuthGuard;
1861
-
1862
- // src/RoleGuard.tsx
1863
- var import_react3 = __toESM(require_react());
1864
- import { useRouter as useRouter2 } from "next/navigation";
1865
-
1866
- // src/myAuth.ts
1867
- var auth = createAppAuth();
1868
-
1869
- // src/RoleGuard.tsx
1870
- var import_jsx_runtime3 = __toESM(require_jsx_runtime());
1871
- var RoleGuard = ({
1872
- children,
1873
- allowedRoles,
1874
- redirectTo = "/unauthorized"
1875
- }) => {
1876
- const { useAuth } = auth;
1877
- const { loading } = useAuth();
1878
- const router = useRouter2();
1879
- const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
1880
- (0, import_react3.useEffect)(() => {
1881
- setIsChecking(false);
1882
- }, [allowedRoles, redirectTo, router]);
1883
- if (loading || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
1884
- return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
1885
- };
1886
- var RoleGuard_default = RoleGuard;
1887
-
1888
- // src/index.ts
1889
- function createAppAuth(storage = "cookie") {
1890
- return createAuthContext({ storage });
1891
- }
1892
1859
  export {
1893
1860
  AuthGuard_default as AuthGuard,
1894
- RoleGuard_default as RoleGuard,
1895
- createAppAuth,
1896
1861
  createAuthContext
1897
1862
  };
1898
1863
  /*! Bundled license information:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nextauthz",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -1,100 +1,155 @@
1
- 'use client'
2
-
3
- import React, {
4
- createContext,
5
- useContext,
6
- ReactNode,
7
- useEffect,
8
- useState,
9
- } from 'react'
1
+ // 'use client'
2
+
3
+ // import React, {
4
+ // createContext,
5
+ // useContext,
6
+ // ReactNode,
7
+ // useEffect,
8
+ // useState,
9
+ // } from 'react'
10
10
  // import { configureTokenManager, useTokenManager } from 'react-token-manager'
11
11
  // import { useAuthStore } from '../store/useGuardStore'
12
12
 
13
- export type AuthContextType<UserType> = {
13
+ // export type AuthContextType<UserType> = {
14
+ // user: UserType | null
15
+ // login: (tokens: Record<string, string>, user: UserType) => void
16
+ // logout: () => void
17
+ // setUser: (user: UserType) => void
18
+ // loading: boolean
19
+ // error: Error | null
20
+ // }
21
+
22
+ // export type AuthContextOptions = {
23
+ // storage?: 'localStorage' | 'sessionStorage' | 'cookie'
24
+ // }
25
+
26
+ // /**
27
+ // * Factory to create AuthProvider and typed useAuth hook
28
+ // */
29
+ // export function createAuthContext<UserType>(options?: AuthContextOptions) {
30
+ // const AuthContext = createContext<AuthContextType<UserType> | null>(null)
31
+
32
+ // const AuthProvider = ({ children }: { children: ReactNode }) => {
33
+ // const storageType = options?.storage || 'cookie'
34
+
35
+ // // Configure token manager once on mount
36
+ // useEffect(() => {
37
+ // configureTokenManager({ storage: storageType })
38
+ // }, [storageType])
39
+
40
+ // // ✅ Hooks must be called inside component
41
+ // const manager = useTokenManager()
42
+
43
+ // const [loading, setLoading] = useState(true)
44
+
45
+ // const rawUser = useAuthStore((state) => state.user)
46
+ // const error = useAuthStore((state) => state.error)
47
+ // const user = rawUser as UserType | null
48
+
49
+ // // Restore saved user from token manager
50
+ // useEffect(() => {
51
+ // const savedUser = manager.getSingleToken('user')
52
+ // if (savedUser) {
53
+ // try {
54
+ // const parsedUser = JSON.parse(savedUser)
55
+ // useAuthStore.getState().setUser(parsedUser)
56
+ // useAuthStore.getState().setError(null)
57
+ // } catch {
58
+ // useAuthStore.getState().resetAuth()
59
+ // useAuthStore.getState().setError(
60
+ // new Error('Failed to parse saved user')
61
+ // )
62
+ // }
63
+ // }
64
+ // setLoading(false)
65
+ // }, [manager])
66
+
67
+ // const setUser = (userData: UserType) => {
68
+ // useAuthStore.getState().setUser(userData as any)
69
+ // useAuthStore.getState().setError(null)
70
+ // manager.setTokens({ user: JSON.stringify(userData) })
71
+ // }
72
+
73
+ // const login = (tokens: Record<string, string>, userData: UserType) => {
74
+ // try {
75
+ // manager.setTokens(tokens)
76
+ // setUser(userData)
77
+ // } catch (err) {
78
+ // useAuthStore.getState().setError(
79
+ // err instanceof Error ? err : new Error(String(err))
80
+ // )
81
+ // }
82
+ // }
83
+
84
+ // const logout = () => {
85
+ // try {
86
+ // manager.clearTokens()
87
+ // useAuthStore.getState().resetAuth()
88
+ // } catch (err) {
89
+ // useAuthStore.getState().setError(
90
+ // err instanceof Error ? err : new Error(String(err))
91
+ // )
92
+ // }
93
+ // }
94
+
95
+ // return (
96
+ // <AuthContext.Provider
97
+ // value={{ user, login, logout, setUser, loading, error }}
98
+ // >
99
+ // {children}
100
+ // </AuthContext.Provider>
101
+ // )
102
+ // }
103
+
104
+ // const useAuth = (): AuthContextType<UserType> => {
105
+ // const ctx = useContext(AuthContext)
106
+ // if (!ctx) throw new Error('useAuth must be used inside AuthProvider')
107
+ // return ctx
108
+ // }
109
+
110
+ // return { AuthProvider, useAuth }
111
+ // }
14
112
 
15
- login: (tokens: Record<string, string>, user: UserType) => void
113
+ 'use client'
114
+
115
+ import React, { createContext, useContext, ReactNode, useState } from 'react'
116
+
117
+ export type AuthContextType<UserType> = {
118
+ user: UserType | null
119
+ login: (userData: UserType) => void
16
120
  logout: () => void
17
- setUser: (user: UserType) => void
121
+ setUser: (userData: UserType) => void
18
122
  loading: boolean
123
+ error: Error | null
19
124
  }
20
125
 
21
- export type AuthContextOptions = {
22
- storage?: 'localStorage' | 'sessionStorage' | 'cookie'
23
- }
24
-
25
- /**
26
- * Factory to create AuthProvider and typed useAuth hook
27
- */
28
- export function createAuthContext<UserType>(options?: AuthContextOptions) {
126
+ export function createAuthContext<UserType>() {
29
127
  const AuthContext = createContext<AuthContextType<UserType> | null>(null)
30
128
 
31
129
  const AuthProvider = ({ children }: { children: ReactNode }) => {
32
- // const storageType = options?.storage || 'cookie'
33
-
34
- // Configure token manager once on mount
35
- // useEffect(() => {
36
- // configureTokenManager({ storage: storageType })
37
- // }, [storageType])
38
-
39
- // ✅ Hooks must be called inside component
40
- // const manager = useTokenManager()
41
-
42
- const [loading, setLoading] = useState(true)
43
-
44
- // const rawUser = useAuthStore((state) => state.user)
45
- // const error = useAuthStore((state) => state.error)
46
- // const user = rawUser as UserType | null
47
-
48
- // Restore saved user from token manager
49
- // useEffect(() => {
50
- // const savedUser = manager.getSingleToken('user')
51
- // if (savedUser) {
52
- // try {
53
- // const parsedUser = JSON.parse(savedUser)
54
- // useAuthStore.getState().setUser(parsedUser)
55
- // useAuthStore.getState().setError(null)
56
- // } catch {
57
- // useAuthStore.getState().resetAuth()
58
- // useAuthStore.getState().setError(
59
- // new Error('Failed to parse saved user')
60
- // )
61
- // }
62
- // }
63
- // setLoading(false)
64
- // }, [manager])
65
-
66
- const setUser = (userData: UserType) => {
67
- // useAuthStore.getState().setUser(userData as any)
68
- // useAuthStore.getState().setError(null)
69
- // manager.setTokens({ user: JSON.stringify(userData) })
70
- }
130
+ const [user, setUser] = useState<UserType | null>(null)
131
+ const [loading, setLoading] = useState(false)
132
+ const [error, setError] = useState<Error | null>(null)
71
133
 
72
- const login = (tokens: Record<string, string>, userData: UserType) => {
134
+ const login = (userData: UserType) => {
135
+ setLoading(true)
73
136
  try {
74
- // manager.setTokens(tokens)
75
137
  setUser(userData)
138
+ setError(null)
76
139
  } catch (err) {
77
- // useAuthStore.getState().setError(
78
- // err instanceof Error ? err : new Error(String(err))
79
- // )
140
+ setError(err instanceof Error ? err : new Error(String(err)))
141
+ } finally {
142
+ setLoading(false)
80
143
  }
81
144
  }
82
145
 
83
146
  const logout = () => {
84
- try {
85
- // manager.clearTokens()
86
- // useAuthStore.getState().resetAuth()
87
- } catch (err) {
88
- // useAuthStore.getState().setError(
89
- // err instanceof Error ? err : new Error(String(err))
90
- // )
91
- }
147
+ setUser(null)
148
+ setError(null)
92
149
  }
93
150
 
94
151
  return (
95
- <AuthContext.Provider
96
- value={{ login, logout, setUser, loading }}
97
- >
152
+ <AuthContext.Provider value={{ user, login, logout, setUser, loading, error }}>
98
153
  {children}
99
154
  </AuthContext.Provider>
100
155
  )
package/src/RoleGuard.tsx CHANGED
@@ -1,40 +1,40 @@
1
- 'use client'
2
-
3
- import React, { useEffect, useState } from 'react'
4
- import { useRouter } from 'next/navigation'
5
- import { auth } from './myAuth'
6
-
7
- type RoleGuardProps = {
8
- children: React.ReactNode
9
- allowedRoles: string[]
10
- redirectTo?: string
11
- }
12
-
13
- const RoleGuard: React.FC<RoleGuardProps> = ({
14
- children,
15
- allowedRoles,
16
- redirectTo = '/unauthorized',
17
- }) => {
18
- // ✅ Destructure useAuth INSIDE the component
19
- const { useAuth } = auth
20
- const { loading } = useAuth()
21
-
22
- const router = useRouter()
23
- const [isChecking, setIsChecking] = useState(true)
24
-
25
- useEffect(() => {
26
- // if (!user) return
27
-
28
- // const hasAccess = allowedRoles.includes(user?.role)
29
- // if (!hasAccess) {
30
- // router.replace(redirectTo)
31
- // }
32
- setIsChecking(false)
33
- }, [allowedRoles, redirectTo, router])
34
-
35
- if (loading || isChecking) return <div>Loading...</div>
36
-
37
- return <>{children}</>
38
- }
39
-
40
- export default RoleGuard
1
+ // 'use client'
2
+
3
+ // import React, { useEffect, useState } from 'react'
4
+ // import { useRouter } from 'next/navigation'
5
+ // import { auth } from './myAuth'
6
+
7
+ // type RoleGuardProps = {
8
+ // children: React.ReactNode
9
+ // allowedRoles: string[]
10
+ // redirectTo?: string
11
+ // }
12
+
13
+ // const RoleGuard: React.FC<RoleGuardProps> = ({
14
+ // children,
15
+ // allowedRoles,
16
+ // redirectTo = '/unauthorized',
17
+ // }) => {
18
+ // // ✅ Destructure useAuth INSIDE the component
19
+ // const { useAuth } = auth
20
+ // const { user, loading } = useAuth()
21
+
22
+ // const router = useRouter()
23
+ // const [isChecking, setIsChecking] = useState(true)
24
+
25
+ // useEffect(() => {
26
+ // if (!user) return
27
+
28
+ // const hasAccess = allowedRoles.includes(user?.role)
29
+ // if (!hasAccess) {
30
+ // router.replace(redirectTo)
31
+ // }
32
+ // setIsChecking(false)
33
+ // }, [user, allowedRoles, redirectTo, router])
34
+
35
+ // if (loading || !user || isChecking) return <div>Loading...</div>
36
+
37
+ // return <>{children}</>
38
+ // }
39
+
40
+ // export default RoleGuard
package/src/index.ts CHANGED
@@ -5,12 +5,12 @@ export type User = {
5
5
  }
6
6
 
7
7
  // Export factory instead of fixed instance
8
- export function createAppAuth(
9
- storage: 'localStorage' | 'sessionStorage' | 'cookie' = 'cookie'
10
- ) {
11
- return createAuthContext<User>({ storage })
12
- }
8
+ // export function createAppAuth(
9
+ // storage: 'localStorage' | 'sessionStorage' | 'cookie' = 'cookie'
10
+ // ) {
11
+ // return createAuthContext<User>({ storage })
12
+ // }
13
13
 
14
14
  export { createAuthContext } from './AuthProvider'
15
15
  export { default as AuthGuard } from './AuthGuard'
16
- export { default as RoleGuard } from './RoleGuard'
16
+ // export { default as RoleGuard } from './RoleGuard'
package/src/myAuth.ts CHANGED
@@ -1,4 +1,4 @@
1
- // myAuth.ts
2
- 'use client'
3
- import { createAppAuth } from '.'
4
- export const auth = createAppAuth() // { AuthProvider, useAuth }
1
+ // // myAuth.ts
2
+ // 'use client'
3
+ // import { createAppAuth } from '.'
4
+ // export const auth = createAppAuth() // { AuthProvider, useAuth }