nextauthz 1.1.0 → 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 +6 -26
- package/dist/index.d.ts +6 -26
- package/dist/index.js +38 -92
- package/dist/index.mjs +38 -90
- package/package.json +1 -1
- package/src/AuthProvider.tsx +128 -74
- package/src/RoleGuard.tsx +40 -40
- package/src/index.ts +6 -6
- package/src/myAuth.ts +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
3
|
-
import React__default, { ReactNode } from 'react';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
4
3
|
|
|
5
4
|
type AuthContextType<UserType> = {
|
|
6
5
|
user: UserType | null;
|
|
7
|
-
login: (
|
|
6
|
+
login: (userData: UserType) => void;
|
|
8
7
|
logout: () => void;
|
|
9
|
-
setUser: (
|
|
8
|
+
setUser: (userData: UserType) => void;
|
|
10
9
|
loading: boolean;
|
|
11
10
|
error: Error | null;
|
|
12
11
|
};
|
|
13
|
-
|
|
14
|
-
storage?: 'localStorage' | 'sessionStorage' | 'cookie';
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Factory to create AuthProvider and typed useAuth hook
|
|
18
|
-
*/
|
|
19
|
-
declare function createAuthContext<UserType>(options?: AuthContextOptions): {
|
|
12
|
+
declare function createAuthContext<UserType>(): {
|
|
20
13
|
AuthProvider: ({ children }: {
|
|
21
14
|
children: ReactNode;
|
|
22
15
|
}) => react_jsx_runtime.JSX.Element;
|
|
@@ -24,28 +17,15 @@ declare function createAuthContext<UserType>(options?: AuthContextOptions): {
|
|
|
24
17
|
};
|
|
25
18
|
|
|
26
19
|
type AuthGuardProps = {
|
|
27
|
-
children:
|
|
20
|
+
children: React.ReactNode;
|
|
28
21
|
redirectTo?: string;
|
|
29
22
|
tokenKey?: string;
|
|
30
23
|
refreshToken?: () => Promise<string | null>;
|
|
31
24
|
};
|
|
32
25
|
declare const AuthGuard: ({ children, redirectTo, tokenKey, refreshToken, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
33
26
|
|
|
34
|
-
type RoleGuardProps = {
|
|
35
|
-
children: React__default.ReactNode;
|
|
36
|
-
allowedRoles: string[];
|
|
37
|
-
redirectTo?: string;
|
|
38
|
-
};
|
|
39
|
-
declare const RoleGuard: React__default.FC<RoleGuardProps>;
|
|
40
|
-
|
|
41
27
|
type User = {
|
|
42
28
|
[key: string]: any;
|
|
43
29
|
};
|
|
44
|
-
declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
|
|
45
|
-
AuthProvider: ({ children }: {
|
|
46
|
-
children: React.ReactNode;
|
|
47
|
-
}) => react_jsx_runtime.JSX.Element;
|
|
48
|
-
useAuth: () => AuthContextType<User>;
|
|
49
|
-
};
|
|
50
30
|
|
|
51
|
-
export { AuthGuard,
|
|
31
|
+
export { AuthGuard, type User, createAuthContext };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,15 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import
|
|
3
|
-
import React__default, { ReactNode } from 'react';
|
|
2
|
+
import React, { ReactNode } from 'react';
|
|
4
3
|
|
|
5
4
|
type AuthContextType<UserType> = {
|
|
6
5
|
user: UserType | null;
|
|
7
|
-
login: (
|
|
6
|
+
login: (userData: UserType) => void;
|
|
8
7
|
logout: () => void;
|
|
9
|
-
setUser: (
|
|
8
|
+
setUser: (userData: UserType) => void;
|
|
10
9
|
loading: boolean;
|
|
11
10
|
error: Error | null;
|
|
12
11
|
};
|
|
13
|
-
|
|
14
|
-
storage?: 'localStorage' | 'sessionStorage' | 'cookie';
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Factory to create AuthProvider and typed useAuth hook
|
|
18
|
-
*/
|
|
19
|
-
declare function createAuthContext<UserType>(options?: AuthContextOptions): {
|
|
12
|
+
declare function createAuthContext<UserType>(): {
|
|
20
13
|
AuthProvider: ({ children }: {
|
|
21
14
|
children: ReactNode;
|
|
22
15
|
}) => react_jsx_runtime.JSX.Element;
|
|
@@ -24,28 +17,15 @@ declare function createAuthContext<UserType>(options?: AuthContextOptions): {
|
|
|
24
17
|
};
|
|
25
18
|
|
|
26
19
|
type AuthGuardProps = {
|
|
27
|
-
children:
|
|
20
|
+
children: React.ReactNode;
|
|
28
21
|
redirectTo?: string;
|
|
29
22
|
tokenKey?: string;
|
|
30
23
|
refreshToken?: () => Promise<string | null>;
|
|
31
24
|
};
|
|
32
25
|
declare const AuthGuard: ({ children, redirectTo, tokenKey, refreshToken, }: AuthGuardProps) => react_jsx_runtime.JSX.Element | null;
|
|
33
26
|
|
|
34
|
-
type RoleGuardProps = {
|
|
35
|
-
children: React__default.ReactNode;
|
|
36
|
-
allowedRoles: string[];
|
|
37
|
-
redirectTo?: string;
|
|
38
|
-
};
|
|
39
|
-
declare const RoleGuard: React__default.FC<RoleGuardProps>;
|
|
40
|
-
|
|
41
27
|
type User = {
|
|
42
28
|
[key: string]: any;
|
|
43
29
|
};
|
|
44
|
-
declare function createAppAuth(storage?: 'localStorage' | 'sessionStorage' | 'cookie'): {
|
|
45
|
-
AuthProvider: ({ children }: {
|
|
46
|
-
children: React.ReactNode;
|
|
47
|
-
}) => react_jsx_runtime.JSX.Element;
|
|
48
|
-
useAuth: () => AuthContextType<User>;
|
|
49
|
-
};
|
|
50
30
|
|
|
51
|
-
export { AuthGuard,
|
|
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
|
|
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
|
-
|
|
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 =
|
|
1713
|
-
|
|
1712
|
+
var unknownOwnerDebugStack = React3.react_stack_bottom_frame.bind(
|
|
1713
|
+
React3,
|
|
1714
1714
|
UnknownOwner
|
|
1715
1715
|
)();
|
|
1716
1716
|
var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));
|
|
@@ -1758,72 +1758,35 @@ 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);
|
|
1766
1764
|
|
|
1767
1765
|
// src/AuthProvider.tsx
|
|
1768
1766
|
var import_react = __toESM(require_react());
|
|
1769
|
-
|
|
1770
|
-
// store/useGuardStore.ts
|
|
1771
|
-
var import_zustand = require("zustand");
|
|
1772
|
-
var useAuthStore = (0, import_zustand.create)((set) => ({
|
|
1773
|
-
user: null,
|
|
1774
|
-
isAuthenticated: false,
|
|
1775
|
-
isAuthChecked: false,
|
|
1776
|
-
error: null,
|
|
1777
|
-
setUser: (user) => set({ user }),
|
|
1778
|
-
setAuth: (isAuth) => set({ isAuthenticated: isAuth }),
|
|
1779
|
-
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
1780
|
-
setError: (err) => set({ error: err }),
|
|
1781
|
-
resetAuth: () => set({
|
|
1782
|
-
user: null,
|
|
1783
|
-
isAuthenticated: false,
|
|
1784
|
-
isAuthChecked: false,
|
|
1785
|
-
error: null
|
|
1786
|
-
})
|
|
1787
|
-
}));
|
|
1788
|
-
|
|
1789
|
-
// src/AuthProvider.tsx
|
|
1790
1767
|
var import_jsx_runtime = __toESM(require_jsx_runtime());
|
|
1791
|
-
function createAuthContext(
|
|
1768
|
+
function createAuthContext() {
|
|
1792
1769
|
const AuthContext = (0, import_react.createContext)(null);
|
|
1793
1770
|
const AuthProvider = ({ children }) => {
|
|
1794
|
-
const [
|
|
1795
|
-
const
|
|
1796
|
-
const error =
|
|
1797
|
-
const
|
|
1798
|
-
|
|
1799
|
-
useAuthStore.getState().setUser(userData);
|
|
1800
|
-
useAuthStore.getState().setError(null);
|
|
1801
|
-
};
|
|
1802
|
-
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);
|
|
1803
1776
|
try {
|
|
1804
1777
|
setUser(userData);
|
|
1778
|
+
setError(null);
|
|
1805
1779
|
} catch (err) {
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
);
|
|
1780
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
1781
|
+
} finally {
|
|
1782
|
+
setLoading(false);
|
|
1809
1783
|
}
|
|
1810
1784
|
};
|
|
1811
1785
|
const logout = () => {
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
} catch (err) {
|
|
1815
|
-
useAuthStore.getState().setError(
|
|
1816
|
-
err instanceof Error ? err : new Error(String(err))
|
|
1817
|
-
);
|
|
1818
|
-
}
|
|
1786
|
+
setUser(null);
|
|
1787
|
+
setError(null);
|
|
1819
1788
|
};
|
|
1820
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1821
|
-
AuthContext.Provider,
|
|
1822
|
-
{
|
|
1823
|
-
value: { user, login, logout, setUser, loading, error },
|
|
1824
|
-
children
|
|
1825
|
-
}
|
|
1826
|
-
);
|
|
1789
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: { user, login, logout, setUser, loading, error }, children });
|
|
1827
1790
|
};
|
|
1828
1791
|
const useAuth = () => {
|
|
1829
1792
|
const ctx = (0, import_react.useContext)(AuthContext);
|
|
@@ -1837,6 +1800,27 @@ function createAuthContext(options) {
|
|
|
1837
1800
|
var import_react2 = __toESM(require_react());
|
|
1838
1801
|
var import_navigation = require("next/navigation");
|
|
1839
1802
|
var import_react_token_manager = require("react-token-manager");
|
|
1803
|
+
|
|
1804
|
+
// store/useGuardStore.ts
|
|
1805
|
+
var import_zustand = require("zustand");
|
|
1806
|
+
var useAuthStore = (0, import_zustand.create)((set) => ({
|
|
1807
|
+
user: null,
|
|
1808
|
+
isAuthenticated: false,
|
|
1809
|
+
isAuthChecked: false,
|
|
1810
|
+
error: null,
|
|
1811
|
+
setUser: (user) => set({ user }),
|
|
1812
|
+
setAuth: (isAuth) => set({ isAuthenticated: isAuth }),
|
|
1813
|
+
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
1814
|
+
setError: (err) => set({ error: err }),
|
|
1815
|
+
resetAuth: () => set({
|
|
1816
|
+
user: null,
|
|
1817
|
+
isAuthenticated: false,
|
|
1818
|
+
isAuthChecked: false,
|
|
1819
|
+
error: null
|
|
1820
|
+
})
|
|
1821
|
+
}));
|
|
1822
|
+
|
|
1823
|
+
// src/AuthGuard.tsx
|
|
1840
1824
|
var import_jsx_runtime2 = __toESM(require_jsx_runtime());
|
|
1841
1825
|
var AuthGuard = ({
|
|
1842
1826
|
children,
|
|
@@ -1886,47 +1870,9 @@ var AuthGuard = ({
|
|
|
1886
1870
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
1887
1871
|
};
|
|
1888
1872
|
var AuthGuard_default = AuthGuard;
|
|
1889
|
-
|
|
1890
|
-
// src/RoleGuard.tsx
|
|
1891
|
-
var import_react3 = __toESM(require_react());
|
|
1892
|
-
var import_navigation2 = require("next/navigation");
|
|
1893
|
-
|
|
1894
|
-
// src/myAuth.ts
|
|
1895
|
-
var auth = createAppAuth();
|
|
1896
|
-
|
|
1897
|
-
// src/RoleGuard.tsx
|
|
1898
|
-
var import_jsx_runtime3 = __toESM(require_jsx_runtime());
|
|
1899
|
-
var RoleGuard = ({
|
|
1900
|
-
children,
|
|
1901
|
-
allowedRoles,
|
|
1902
|
-
redirectTo = "/unauthorized"
|
|
1903
|
-
}) => {
|
|
1904
|
-
const { useAuth } = auth;
|
|
1905
|
-
const { user, loading } = useAuth();
|
|
1906
|
-
const router = (0, import_navigation2.useRouter)();
|
|
1907
|
-
const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
|
|
1908
|
-
(0, import_react3.useEffect)(() => {
|
|
1909
|
-
if (!user) return;
|
|
1910
|
-
const hasAccess = allowedRoles.includes(user?.role);
|
|
1911
|
-
if (!hasAccess) {
|
|
1912
|
-
router.replace(redirectTo);
|
|
1913
|
-
}
|
|
1914
|
-
setIsChecking(false);
|
|
1915
|
-
}, [user, allowedRoles, redirectTo, router]);
|
|
1916
|
-
if (loading || !user || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
|
|
1917
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
1918
|
-
};
|
|
1919
|
-
var RoleGuard_default = RoleGuard;
|
|
1920
|
-
|
|
1921
|
-
// src/index.ts
|
|
1922
|
-
function createAppAuth(storage = "cookie") {
|
|
1923
|
-
return createAuthContext({ storage });
|
|
1924
|
-
}
|
|
1925
1873
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1926
1874
|
0 && (module.exports = {
|
|
1927
1875
|
AuthGuard,
|
|
1928
|
-
RoleGuard,
|
|
1929
|
-
createAppAuth,
|
|
1930
1876
|
createAuthContext
|
|
1931
1877
|
});
|
|
1932
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
|
|
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
|
-
|
|
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 =
|
|
1707
|
-
|
|
1706
|
+
var unknownOwnerDebugStack = React3.react_stack_bottom_frame.bind(
|
|
1707
|
+
React3,
|
|
1708
1708
|
UnknownOwner
|
|
1709
1709
|
)();
|
|
1710
1710
|
var unknownOwnerDebugTask = createTask(getTaskName(UnknownOwner));
|
|
@@ -1750,64 +1750,29 @@ var require_jsx_runtime = __commonJS({
|
|
|
1750
1750
|
|
|
1751
1751
|
// src/AuthProvider.tsx
|
|
1752
1752
|
var import_react = __toESM(require_react());
|
|
1753
|
-
|
|
1754
|
-
// store/useGuardStore.ts
|
|
1755
|
-
import { create } from "zustand";
|
|
1756
|
-
var useAuthStore = create((set) => ({
|
|
1757
|
-
user: null,
|
|
1758
|
-
isAuthenticated: false,
|
|
1759
|
-
isAuthChecked: false,
|
|
1760
|
-
error: null,
|
|
1761
|
-
setUser: (user) => set({ user }),
|
|
1762
|
-
setAuth: (isAuth) => set({ isAuthenticated: isAuth }),
|
|
1763
|
-
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
1764
|
-
setError: (err) => set({ error: err }),
|
|
1765
|
-
resetAuth: () => set({
|
|
1766
|
-
user: null,
|
|
1767
|
-
isAuthenticated: false,
|
|
1768
|
-
isAuthChecked: false,
|
|
1769
|
-
error: null
|
|
1770
|
-
})
|
|
1771
|
-
}));
|
|
1772
|
-
|
|
1773
|
-
// src/AuthProvider.tsx
|
|
1774
1753
|
var import_jsx_runtime = __toESM(require_jsx_runtime());
|
|
1775
|
-
function createAuthContext(
|
|
1754
|
+
function createAuthContext() {
|
|
1776
1755
|
const AuthContext = (0, import_react.createContext)(null);
|
|
1777
1756
|
const AuthProvider = ({ children }) => {
|
|
1778
|
-
const [
|
|
1779
|
-
const
|
|
1780
|
-
const error =
|
|
1781
|
-
const
|
|
1782
|
-
|
|
1783
|
-
useAuthStore.getState().setUser(userData);
|
|
1784
|
-
useAuthStore.getState().setError(null);
|
|
1785
|
-
};
|
|
1786
|
-
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);
|
|
1787
1762
|
try {
|
|
1788
1763
|
setUser(userData);
|
|
1764
|
+
setError(null);
|
|
1789
1765
|
} catch (err) {
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
);
|
|
1766
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
1767
|
+
} finally {
|
|
1768
|
+
setLoading(false);
|
|
1793
1769
|
}
|
|
1794
1770
|
};
|
|
1795
1771
|
const logout = () => {
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
} catch (err) {
|
|
1799
|
-
useAuthStore.getState().setError(
|
|
1800
|
-
err instanceof Error ? err : new Error(String(err))
|
|
1801
|
-
);
|
|
1802
|
-
}
|
|
1772
|
+
setUser(null);
|
|
1773
|
+
setError(null);
|
|
1803
1774
|
};
|
|
1804
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1805
|
-
AuthContext.Provider,
|
|
1806
|
-
{
|
|
1807
|
-
value: { user, login, logout, setUser, loading, error },
|
|
1808
|
-
children
|
|
1809
|
-
}
|
|
1810
|
-
);
|
|
1775
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AuthContext.Provider, { value: { user, login, logout, setUser, loading, error }, children });
|
|
1811
1776
|
};
|
|
1812
1777
|
const useAuth = () => {
|
|
1813
1778
|
const ctx = (0, import_react.useContext)(AuthContext);
|
|
@@ -1821,6 +1786,27 @@ function createAuthContext(options) {
|
|
|
1821
1786
|
var import_react2 = __toESM(require_react());
|
|
1822
1787
|
import { useRouter } from "next/navigation";
|
|
1823
1788
|
import { useTokenManager } from "react-token-manager";
|
|
1789
|
+
|
|
1790
|
+
// store/useGuardStore.ts
|
|
1791
|
+
import { create } from "zustand";
|
|
1792
|
+
var useAuthStore = create((set) => ({
|
|
1793
|
+
user: null,
|
|
1794
|
+
isAuthenticated: false,
|
|
1795
|
+
isAuthChecked: false,
|
|
1796
|
+
error: null,
|
|
1797
|
+
setUser: (user) => set({ user }),
|
|
1798
|
+
setAuth: (isAuth) => set({ isAuthenticated: isAuth }),
|
|
1799
|
+
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
1800
|
+
setError: (err) => set({ error: err }),
|
|
1801
|
+
resetAuth: () => set({
|
|
1802
|
+
user: null,
|
|
1803
|
+
isAuthenticated: false,
|
|
1804
|
+
isAuthChecked: false,
|
|
1805
|
+
error: null
|
|
1806
|
+
})
|
|
1807
|
+
}));
|
|
1808
|
+
|
|
1809
|
+
// src/AuthGuard.tsx
|
|
1824
1810
|
var import_jsx_runtime2 = __toESM(require_jsx_runtime());
|
|
1825
1811
|
var AuthGuard = ({
|
|
1826
1812
|
children,
|
|
@@ -1870,46 +1856,8 @@ var AuthGuard = ({
|
|
|
1870
1856
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
1871
1857
|
};
|
|
1872
1858
|
var AuthGuard_default = AuthGuard;
|
|
1873
|
-
|
|
1874
|
-
// src/RoleGuard.tsx
|
|
1875
|
-
var import_react3 = __toESM(require_react());
|
|
1876
|
-
import { useRouter as useRouter2 } from "next/navigation";
|
|
1877
|
-
|
|
1878
|
-
// src/myAuth.ts
|
|
1879
|
-
var auth = createAppAuth();
|
|
1880
|
-
|
|
1881
|
-
// src/RoleGuard.tsx
|
|
1882
|
-
var import_jsx_runtime3 = __toESM(require_jsx_runtime());
|
|
1883
|
-
var RoleGuard = ({
|
|
1884
|
-
children,
|
|
1885
|
-
allowedRoles,
|
|
1886
|
-
redirectTo = "/unauthorized"
|
|
1887
|
-
}) => {
|
|
1888
|
-
const { useAuth } = auth;
|
|
1889
|
-
const { user, loading } = useAuth();
|
|
1890
|
-
const router = useRouter2();
|
|
1891
|
-
const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
|
|
1892
|
-
(0, import_react3.useEffect)(() => {
|
|
1893
|
-
if (!user) return;
|
|
1894
|
-
const hasAccess = allowedRoles.includes(user?.role);
|
|
1895
|
-
if (!hasAccess) {
|
|
1896
|
-
router.replace(redirectTo);
|
|
1897
|
-
}
|
|
1898
|
-
setIsChecking(false);
|
|
1899
|
-
}, [user, allowedRoles, redirectTo, router]);
|
|
1900
|
-
if (loading || !user || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
|
|
1901
|
-
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
1902
|
-
};
|
|
1903
|
-
var RoleGuard_default = RoleGuard;
|
|
1904
|
-
|
|
1905
|
-
// src/index.ts
|
|
1906
|
-
function createAppAuth(storage = "cookie") {
|
|
1907
|
-
return createAuthContext({ storage });
|
|
1908
|
-
}
|
|
1909
1859
|
export {
|
|
1910
1860
|
AuthGuard_default as AuthGuard,
|
|
1911
|
-
RoleGuard_default as RoleGuard,
|
|
1912
|
-
createAppAuth,
|
|
1913
1861
|
createAuthContext
|
|
1914
1862
|
};
|
|
1915
1863
|
/*! Bundled license information:
|
package/package.json
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -1,101 +1,155 @@
|
|
|
1
|
+
// 'use client'
|
|
2
|
+
|
|
3
|
+
// import React, {
|
|
4
|
+
// createContext,
|
|
5
|
+
// useContext,
|
|
6
|
+
// ReactNode,
|
|
7
|
+
// useEffect,
|
|
8
|
+
// useState,
|
|
9
|
+
// } from 'react'
|
|
10
|
+
// import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
11
|
+
// import { useAuthStore } from '../store/useGuardStore'
|
|
12
|
+
|
|
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
|
+
// }
|
|
112
|
+
|
|
1
113
|
'use client'
|
|
2
114
|
|
|
3
|
-
import React, {
|
|
4
|
-
createContext,
|
|
5
|
-
useContext,
|
|
6
|
-
ReactNode,
|
|
7
|
-
useEffect,
|
|
8
|
-
useState,
|
|
9
|
-
} from 'react'
|
|
10
|
-
// import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
11
|
-
import { useAuthStore } from '../store/useGuardStore'
|
|
115
|
+
import React, { createContext, useContext, ReactNode, useState } from 'react'
|
|
12
116
|
|
|
13
117
|
export type AuthContextType<UserType> = {
|
|
14
118
|
user: UserType | null
|
|
15
|
-
login: (
|
|
119
|
+
login: (userData: UserType) => void
|
|
16
120
|
logout: () => void
|
|
17
|
-
setUser: (
|
|
121
|
+
setUser: (userData: UserType) => void
|
|
18
122
|
loading: boolean
|
|
19
123
|
error: Error | null
|
|
20
124
|
}
|
|
21
125
|
|
|
22
|
-
export
|
|
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) {
|
|
126
|
+
export function createAuthContext<UserType>() {
|
|
30
127
|
const AuthContext = createContext<AuthContextType<UserType> | null>(null)
|
|
31
128
|
|
|
32
129
|
const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
}
|
|
130
|
+
const [user, setUser] = useState<UserType | null>(null)
|
|
131
|
+
const [loading, setLoading] = useState(false)
|
|
132
|
+
const [error, setError] = useState<Error | null>(null)
|
|
72
133
|
|
|
73
|
-
const login = (
|
|
134
|
+
const login = (userData: UserType) => {
|
|
135
|
+
setLoading(true)
|
|
74
136
|
try {
|
|
75
|
-
// manager.setTokens(tokens)
|
|
76
137
|
setUser(userData)
|
|
138
|
+
setError(null)
|
|
77
139
|
} catch (err) {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
)
|
|
140
|
+
setError(err instanceof Error ? err : new Error(String(err)))
|
|
141
|
+
} finally {
|
|
142
|
+
setLoading(false)
|
|
81
143
|
}
|
|
82
144
|
}
|
|
83
145
|
|
|
84
146
|
const logout = () => {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
useAuthStore.getState().resetAuth()
|
|
88
|
-
} catch (err) {
|
|
89
|
-
useAuthStore.getState().setError(
|
|
90
|
-
err instanceof Error ? err : new Error(String(err))
|
|
91
|
-
)
|
|
92
|
-
}
|
|
147
|
+
setUser(null)
|
|
148
|
+
setError(null)
|
|
93
149
|
}
|
|
94
150
|
|
|
95
151
|
return (
|
|
96
|
-
<AuthContext.Provider
|
|
97
|
-
value={{ user, login, logout, setUser, loading, error }}
|
|
98
|
-
>
|
|
152
|
+
<AuthContext.Provider value={{ user, login, logout, setUser, loading, error }}>
|
|
99
153
|
{children}
|
|
100
154
|
</AuthContext.Provider>
|
|
101
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const RoleGuard: React.FC<RoleGuardProps> = ({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
10
|
-
) {
|
|
11
|
-
|
|
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 }
|