nextauthz 1.1.0 → 1.1.1
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 +0 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +25 -42
- package/dist/index.mjs +25 -42
- package/package.json +1 -1
- package/src/AuthProvider.tsx +15 -16
- package/src/RoleGuard.tsx +8 -8
package/dist/index.d.mts
CHANGED
|
@@ -3,12 +3,10 @@ import * as React from 'react';
|
|
|
3
3
|
import React__default, { ReactNode } from 'react';
|
|
4
4
|
|
|
5
5
|
type AuthContextType<UserType> = {
|
|
6
|
-
user: UserType | null;
|
|
7
6
|
login: (tokens: Record<string, string>, user: UserType) => void;
|
|
8
7
|
logout: () => void;
|
|
9
8
|
setUser: (user: UserType) => void;
|
|
10
9
|
loading: boolean;
|
|
11
|
-
error: Error | null;
|
|
12
10
|
};
|
|
13
11
|
type AuthContextOptions = {
|
|
14
12
|
storage?: 'localStorage' | 'sessionStorage' | 'cookie';
|
package/dist/index.d.ts
CHANGED
|
@@ -3,12 +3,10 @@ import * as React from 'react';
|
|
|
3
3
|
import React__default, { ReactNode } from 'react';
|
|
4
4
|
|
|
5
5
|
type AuthContextType<UserType> = {
|
|
6
|
-
user: UserType | null;
|
|
7
6
|
login: (tokens: Record<string, string>, user: UserType) => void;
|
|
8
7
|
logout: () => void;
|
|
9
8
|
setUser: (user: UserType) => void;
|
|
10
9
|
loading: boolean;
|
|
11
|
-
error: Error | null;
|
|
12
10
|
};
|
|
13
11
|
type AuthContextOptions = {
|
|
14
12
|
storage?: 'localStorage' | 'sessionStorage' | 'cookie';
|
package/dist/index.js
CHANGED
|
@@ -1766,61 +1766,28 @@ module.exports = __toCommonJS(index_exports);
|
|
|
1766
1766
|
|
|
1767
1767
|
// src/AuthProvider.tsx
|
|
1768
1768
|
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
1769
|
var import_jsx_runtime = __toESM(require_jsx_runtime());
|
|
1791
1770
|
function createAuthContext(options) {
|
|
1792
1771
|
const AuthContext = (0, import_react.createContext)(null);
|
|
1793
1772
|
const AuthProvider = ({ children }) => {
|
|
1794
1773
|
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
1795
|
-
const rawUser = useAuthStore((state) => state.user);
|
|
1796
|
-
const error = useAuthStore((state) => state.error);
|
|
1797
|
-
const user = rawUser;
|
|
1798
1774
|
const setUser = (userData) => {
|
|
1799
|
-
useAuthStore.getState().setUser(userData);
|
|
1800
|
-
useAuthStore.getState().setError(null);
|
|
1801
1775
|
};
|
|
1802
1776
|
const login = (tokens, userData) => {
|
|
1803
1777
|
try {
|
|
1804
1778
|
setUser(userData);
|
|
1805
1779
|
} catch (err) {
|
|
1806
|
-
useAuthStore.getState().setError(
|
|
1807
|
-
err instanceof Error ? err : new Error(String(err))
|
|
1808
|
-
);
|
|
1809
1780
|
}
|
|
1810
1781
|
};
|
|
1811
1782
|
const logout = () => {
|
|
1812
1783
|
try {
|
|
1813
|
-
useAuthStore.getState().resetAuth();
|
|
1814
1784
|
} catch (err) {
|
|
1815
|
-
useAuthStore.getState().setError(
|
|
1816
|
-
err instanceof Error ? err : new Error(String(err))
|
|
1817
|
-
);
|
|
1818
1785
|
}
|
|
1819
1786
|
};
|
|
1820
1787
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1821
1788
|
AuthContext.Provider,
|
|
1822
1789
|
{
|
|
1823
|
-
value: {
|
|
1790
|
+
value: { login, logout, setUser, loading },
|
|
1824
1791
|
children
|
|
1825
1792
|
}
|
|
1826
1793
|
);
|
|
@@ -1837,6 +1804,27 @@ function createAuthContext(options) {
|
|
|
1837
1804
|
var import_react2 = __toESM(require_react());
|
|
1838
1805
|
var import_navigation = require("next/navigation");
|
|
1839
1806
|
var import_react_token_manager = require("react-token-manager");
|
|
1807
|
+
|
|
1808
|
+
// store/useGuardStore.ts
|
|
1809
|
+
var import_zustand = require("zustand");
|
|
1810
|
+
var useAuthStore = (0, import_zustand.create)((set) => ({
|
|
1811
|
+
user: null,
|
|
1812
|
+
isAuthenticated: false,
|
|
1813
|
+
isAuthChecked: false,
|
|
1814
|
+
error: null,
|
|
1815
|
+
setUser: (user) => set({ user }),
|
|
1816
|
+
setAuth: (isAuth) => set({ isAuthenticated: isAuth }),
|
|
1817
|
+
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
1818
|
+
setError: (err) => set({ error: err }),
|
|
1819
|
+
resetAuth: () => set({
|
|
1820
|
+
user: null,
|
|
1821
|
+
isAuthenticated: false,
|
|
1822
|
+
isAuthChecked: false,
|
|
1823
|
+
error: null
|
|
1824
|
+
})
|
|
1825
|
+
}));
|
|
1826
|
+
|
|
1827
|
+
// src/AuthGuard.tsx
|
|
1840
1828
|
var import_jsx_runtime2 = __toESM(require_jsx_runtime());
|
|
1841
1829
|
var AuthGuard = ({
|
|
1842
1830
|
children,
|
|
@@ -1902,18 +1890,13 @@ var RoleGuard = ({
|
|
|
1902
1890
|
redirectTo = "/unauthorized"
|
|
1903
1891
|
}) => {
|
|
1904
1892
|
const { useAuth } = auth;
|
|
1905
|
-
const {
|
|
1893
|
+
const { loading } = useAuth();
|
|
1906
1894
|
const router = (0, import_navigation2.useRouter)();
|
|
1907
1895
|
const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
|
|
1908
1896
|
(0, import_react3.useEffect)(() => {
|
|
1909
|
-
if (!user) return;
|
|
1910
|
-
const hasAccess = allowedRoles.includes(user?.role);
|
|
1911
|
-
if (!hasAccess) {
|
|
1912
|
-
router.replace(redirectTo);
|
|
1913
|
-
}
|
|
1914
1897
|
setIsChecking(false);
|
|
1915
|
-
}, [
|
|
1916
|
-
if (loading ||
|
|
1898
|
+
}, [allowedRoles, redirectTo, router]);
|
|
1899
|
+
if (loading || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
|
|
1917
1900
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
1918
1901
|
};
|
|
1919
1902
|
var RoleGuard_default = RoleGuard;
|
package/dist/index.mjs
CHANGED
|
@@ -1750,61 +1750,28 @@ 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
1754
|
function createAuthContext(options) {
|
|
1776
1755
|
const AuthContext = (0, import_react.createContext)(null);
|
|
1777
1756
|
const AuthProvider = ({ children }) => {
|
|
1778
1757
|
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
1779
|
-
const rawUser = useAuthStore((state) => state.user);
|
|
1780
|
-
const error = useAuthStore((state) => state.error);
|
|
1781
|
-
const user = rawUser;
|
|
1782
1758
|
const setUser = (userData) => {
|
|
1783
|
-
useAuthStore.getState().setUser(userData);
|
|
1784
|
-
useAuthStore.getState().setError(null);
|
|
1785
1759
|
};
|
|
1786
1760
|
const login = (tokens, userData) => {
|
|
1787
1761
|
try {
|
|
1788
1762
|
setUser(userData);
|
|
1789
1763
|
} catch (err) {
|
|
1790
|
-
useAuthStore.getState().setError(
|
|
1791
|
-
err instanceof Error ? err : new Error(String(err))
|
|
1792
|
-
);
|
|
1793
1764
|
}
|
|
1794
1765
|
};
|
|
1795
1766
|
const logout = () => {
|
|
1796
1767
|
try {
|
|
1797
|
-
useAuthStore.getState().resetAuth();
|
|
1798
1768
|
} catch (err) {
|
|
1799
|
-
useAuthStore.getState().setError(
|
|
1800
|
-
err instanceof Error ? err : new Error(String(err))
|
|
1801
|
-
);
|
|
1802
1769
|
}
|
|
1803
1770
|
};
|
|
1804
1771
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1805
1772
|
AuthContext.Provider,
|
|
1806
1773
|
{
|
|
1807
|
-
value: {
|
|
1774
|
+
value: { login, logout, setUser, loading },
|
|
1808
1775
|
children
|
|
1809
1776
|
}
|
|
1810
1777
|
);
|
|
@@ -1821,6 +1788,27 @@ function createAuthContext(options) {
|
|
|
1821
1788
|
var import_react2 = __toESM(require_react());
|
|
1822
1789
|
import { useRouter } from "next/navigation";
|
|
1823
1790
|
import { useTokenManager } from "react-token-manager";
|
|
1791
|
+
|
|
1792
|
+
// store/useGuardStore.ts
|
|
1793
|
+
import { create } from "zustand";
|
|
1794
|
+
var useAuthStore = create((set) => ({
|
|
1795
|
+
user: null,
|
|
1796
|
+
isAuthenticated: false,
|
|
1797
|
+
isAuthChecked: false,
|
|
1798
|
+
error: null,
|
|
1799
|
+
setUser: (user) => set({ user }),
|
|
1800
|
+
setAuth: (isAuth) => set({ isAuthenticated: isAuth }),
|
|
1801
|
+
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
1802
|
+
setError: (err) => set({ error: err }),
|
|
1803
|
+
resetAuth: () => set({
|
|
1804
|
+
user: null,
|
|
1805
|
+
isAuthenticated: false,
|
|
1806
|
+
isAuthChecked: false,
|
|
1807
|
+
error: null
|
|
1808
|
+
})
|
|
1809
|
+
}));
|
|
1810
|
+
|
|
1811
|
+
// src/AuthGuard.tsx
|
|
1824
1812
|
var import_jsx_runtime2 = __toESM(require_jsx_runtime());
|
|
1825
1813
|
var AuthGuard = ({
|
|
1826
1814
|
children,
|
|
@@ -1886,18 +1874,13 @@ var RoleGuard = ({
|
|
|
1886
1874
|
redirectTo = "/unauthorized"
|
|
1887
1875
|
}) => {
|
|
1888
1876
|
const { useAuth } = auth;
|
|
1889
|
-
const {
|
|
1877
|
+
const { loading } = useAuth();
|
|
1890
1878
|
const router = useRouter2();
|
|
1891
1879
|
const [isChecking, setIsChecking] = (0, import_react3.useState)(true);
|
|
1892
1880
|
(0, import_react3.useEffect)(() => {
|
|
1893
|
-
if (!user) return;
|
|
1894
|
-
const hasAccess = allowedRoles.includes(user?.role);
|
|
1895
|
-
if (!hasAccess) {
|
|
1896
|
-
router.replace(redirectTo);
|
|
1897
|
-
}
|
|
1898
1881
|
setIsChecking(false);
|
|
1899
|
-
}, [
|
|
1900
|
-
if (loading ||
|
|
1882
|
+
}, [allowedRoles, redirectTo, router]);
|
|
1883
|
+
if (loading || isChecking) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { children: "Loading..." });
|
|
1901
1884
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
1902
1885
|
};
|
|
1903
1886
|
var RoleGuard_default = RoleGuard;
|
package/package.json
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -8,15 +8,14 @@ import React, {
|
|
|
8
8
|
useState,
|
|
9
9
|
} from 'react'
|
|
10
10
|
// import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
11
|
-
import { useAuthStore } from '../store/useGuardStore'
|
|
11
|
+
// import { useAuthStore } from '../store/useGuardStore'
|
|
12
12
|
|
|
13
13
|
export type AuthContextType<UserType> = {
|
|
14
|
-
|
|
14
|
+
|
|
15
15
|
login: (tokens: Record<string, string>, user: UserType) => void
|
|
16
16
|
logout: () => void
|
|
17
17
|
setUser: (user: UserType) => void
|
|
18
18
|
loading: boolean
|
|
19
|
-
error: Error | null
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
export type AuthContextOptions = {
|
|
@@ -42,9 +41,9 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
42
41
|
|
|
43
42
|
const [loading, setLoading] = useState(true)
|
|
44
43
|
|
|
45
|
-
const rawUser = useAuthStore((state) => state.user)
|
|
46
|
-
const error = useAuthStore((state) => state.error)
|
|
47
|
-
const user = rawUser as UserType | null
|
|
44
|
+
// const rawUser = useAuthStore((state) => state.user)
|
|
45
|
+
// const error = useAuthStore((state) => state.error)
|
|
46
|
+
// const user = rawUser as UserType | null
|
|
48
47
|
|
|
49
48
|
// Restore saved user from token manager
|
|
50
49
|
// useEffect(() => {
|
|
@@ -65,8 +64,8 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
65
64
|
// }, [manager])
|
|
66
65
|
|
|
67
66
|
const setUser = (userData: UserType) => {
|
|
68
|
-
useAuthStore.getState().setUser(userData as any)
|
|
69
|
-
useAuthStore.getState().setError(null)
|
|
67
|
+
// useAuthStore.getState().setUser(userData as any)
|
|
68
|
+
// useAuthStore.getState().setError(null)
|
|
70
69
|
// manager.setTokens({ user: JSON.stringify(userData) })
|
|
71
70
|
}
|
|
72
71
|
|
|
@@ -75,26 +74,26 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
75
74
|
// manager.setTokens(tokens)
|
|
76
75
|
setUser(userData)
|
|
77
76
|
} catch (err) {
|
|
78
|
-
useAuthStore.getState().setError(
|
|
79
|
-
|
|
80
|
-
)
|
|
77
|
+
// useAuthStore.getState().setError(
|
|
78
|
+
// err instanceof Error ? err : new Error(String(err))
|
|
79
|
+
// )
|
|
81
80
|
}
|
|
82
81
|
}
|
|
83
82
|
|
|
84
83
|
const logout = () => {
|
|
85
84
|
try {
|
|
86
85
|
// manager.clearTokens()
|
|
87
|
-
useAuthStore.getState().resetAuth()
|
|
86
|
+
// useAuthStore.getState().resetAuth()
|
|
88
87
|
} catch (err) {
|
|
89
|
-
useAuthStore.getState().setError(
|
|
90
|
-
|
|
91
|
-
)
|
|
88
|
+
// useAuthStore.getState().setError(
|
|
89
|
+
// err instanceof Error ? err : new Error(String(err))
|
|
90
|
+
// )
|
|
92
91
|
}
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
return (
|
|
96
95
|
<AuthContext.Provider
|
|
97
|
-
value={{
|
|
96
|
+
value={{ login, logout, setUser, loading }}
|
|
98
97
|
>
|
|
99
98
|
{children}
|
|
100
99
|
</AuthContext.Provider>
|
package/src/RoleGuard.tsx
CHANGED
|
@@ -17,22 +17,22 @@ const RoleGuard: React.FC<RoleGuardProps> = ({
|
|
|
17
17
|
}) => {
|
|
18
18
|
// ✅ Destructure useAuth INSIDE the component
|
|
19
19
|
const { useAuth } = auth
|
|
20
|
-
const {
|
|
20
|
+
const { loading } = useAuth()
|
|
21
21
|
|
|
22
22
|
const router = useRouter()
|
|
23
23
|
const [isChecking, setIsChecking] = useState(true)
|
|
24
24
|
|
|
25
25
|
useEffect(() => {
|
|
26
|
-
if (!user) return
|
|
26
|
+
// if (!user) return
|
|
27
27
|
|
|
28
|
-
const hasAccess = allowedRoles.includes(user?.role)
|
|
29
|
-
if (!hasAccess) {
|
|
30
|
-
|
|
31
|
-
}
|
|
28
|
+
// const hasAccess = allowedRoles.includes(user?.role)
|
|
29
|
+
// if (!hasAccess) {
|
|
30
|
+
// router.replace(redirectTo)
|
|
31
|
+
// }
|
|
32
32
|
setIsChecking(false)
|
|
33
|
-
}, [
|
|
33
|
+
}, [allowedRoles, redirectTo, router])
|
|
34
34
|
|
|
35
|
-
if (loading ||
|
|
35
|
+
if (loading || isChecking) return <div>Loading...</div>
|
|
36
36
|
|
|
37
37
|
return <>{children}</>
|
|
38
38
|
}
|