nextauthz 1.0.2 → 1.0.4
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +28 -11
- package/dist/index.mjs +28 -11
- package/package.json +1 -1
- package/src/AuthProvider.tsx +20 -26
package/dist/index.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import * as React from 'react';
|
|
|
3
3
|
import React__default, { ReactNode } from 'react';
|
|
4
4
|
|
|
5
5
|
type AuthContextType<UserType> = {
|
|
6
|
-
user: UserType |
|
|
6
|
+
user: UserType | null;
|
|
7
7
|
login: (tokens: Record<string, string>, user: UserType) => void;
|
|
8
8
|
logout: () => void;
|
|
9
9
|
setUser: (user: UserType) => void;
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import * as React from 'react';
|
|
|
3
3
|
import React__default, { ReactNode } from 'react';
|
|
4
4
|
|
|
5
5
|
type AuthContextType<UserType> = {
|
|
6
|
-
user: UserType |
|
|
6
|
+
user: UserType | null;
|
|
7
7
|
login: (tokens: Record<string, string>, user: UserType) => void;
|
|
8
8
|
logout: () => void;
|
|
9
9
|
setUser: (user: UserType) => void;
|
package/dist/index.js
CHANGED
|
@@ -1790,14 +1790,17 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
|
|
|
1790
1790
|
// src/AuthProvider.tsx
|
|
1791
1791
|
var import_jsx_runtime = __toESM(require_jsx_runtime());
|
|
1792
1792
|
function createAuthContext(options) {
|
|
1793
|
-
const storageType = options?.storage || "cookie";
|
|
1794
|
-
(0, import_react_token_manager.configureTokenManager)({ storage: storageType });
|
|
1795
|
-
const manager = (0, import_react_token_manager.useTokenManager)();
|
|
1796
1793
|
const AuthContext = (0, import_react.createContext)(null);
|
|
1797
1794
|
const AuthProvider2 = ({ children }) => {
|
|
1795
|
+
const storageType = options?.storage || "cookie";
|
|
1796
|
+
(0, import_react.useEffect)(() => {
|
|
1797
|
+
(0, import_react_token_manager.configureTokenManager)({ storage: storageType });
|
|
1798
|
+
}, [storageType]);
|
|
1799
|
+
const manager = (0, import_react_token_manager.useTokenManager)();
|
|
1798
1800
|
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
1799
|
-
const
|
|
1801
|
+
const rawUser = useAuthStore((state) => state.user);
|
|
1800
1802
|
const error = useAuthStore((state) => state.error);
|
|
1803
|
+
const user = rawUser;
|
|
1801
1804
|
(0, import_react.useEffect)(() => {
|
|
1802
1805
|
const savedUser = manager.getSingleToken("user");
|
|
1803
1806
|
if (savedUser) {
|
|
@@ -1805,13 +1808,15 @@ function createAuthContext(options) {
|
|
|
1805
1808
|
const parsedUser = JSON.parse(savedUser);
|
|
1806
1809
|
useAuthStore.getState().setUser(parsedUser);
|
|
1807
1810
|
useAuthStore.getState().setError(null);
|
|
1808
|
-
} catch
|
|
1811
|
+
} catch {
|
|
1809
1812
|
useAuthStore.getState().resetAuth();
|
|
1810
|
-
useAuthStore.getState().setError(
|
|
1813
|
+
useAuthStore.getState().setError(
|
|
1814
|
+
new Error("Failed to parse saved user")
|
|
1815
|
+
);
|
|
1811
1816
|
}
|
|
1812
1817
|
}
|
|
1813
1818
|
setLoading(false);
|
|
1814
|
-
}, []);
|
|
1819
|
+
}, [manager]);
|
|
1815
1820
|
const setUser = (userData) => {
|
|
1816
1821
|
useAuthStore.getState().setUser(userData);
|
|
1817
1822
|
useAuthStore.getState().setError(null);
|
|
@@ -1822,7 +1827,9 @@ function createAuthContext(options) {
|
|
|
1822
1827
|
manager.setTokens(tokens);
|
|
1823
1828
|
setUser(userData);
|
|
1824
1829
|
} catch (err) {
|
|
1825
|
-
useAuthStore.getState().setError(
|
|
1830
|
+
useAuthStore.getState().setError(
|
|
1831
|
+
err instanceof Error ? err : new Error(String(err))
|
|
1832
|
+
);
|
|
1826
1833
|
}
|
|
1827
1834
|
};
|
|
1828
1835
|
const logout = () => {
|
|
@@ -1830,14 +1837,24 @@ function createAuthContext(options) {
|
|
|
1830
1837
|
manager.clearTokens();
|
|
1831
1838
|
useAuthStore.getState().resetAuth();
|
|
1832
1839
|
} catch (err) {
|
|
1833
|
-
useAuthStore.getState().setError(
|
|
1840
|
+
useAuthStore.getState().setError(
|
|
1841
|
+
err instanceof Error ? err : new Error(String(err))
|
|
1842
|
+
);
|
|
1834
1843
|
}
|
|
1835
1844
|
};
|
|
1836
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1845
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1846
|
+
AuthContext.Provider,
|
|
1847
|
+
{
|
|
1848
|
+
value: { user, login, logout, setUser, loading, error },
|
|
1849
|
+
children
|
|
1850
|
+
}
|
|
1851
|
+
);
|
|
1837
1852
|
};
|
|
1838
1853
|
const useAuth2 = () => {
|
|
1839
1854
|
const ctx = (0, import_react.useContext)(AuthContext);
|
|
1840
|
-
if (!ctx)
|
|
1855
|
+
if (!ctx) {
|
|
1856
|
+
throw new Error("useAuth must be used inside AuthProvider");
|
|
1857
|
+
}
|
|
1841
1858
|
return ctx;
|
|
1842
1859
|
};
|
|
1843
1860
|
return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
|
package/dist/index.mjs
CHANGED
|
@@ -1774,14 +1774,17 @@ var useAuthStore = create((set) => ({
|
|
|
1774
1774
|
// src/AuthProvider.tsx
|
|
1775
1775
|
var import_jsx_runtime = __toESM(require_jsx_runtime());
|
|
1776
1776
|
function createAuthContext(options) {
|
|
1777
|
-
const storageType = options?.storage || "cookie";
|
|
1778
|
-
configureTokenManager({ storage: storageType });
|
|
1779
|
-
const manager = useTokenManager();
|
|
1780
1777
|
const AuthContext = (0, import_react.createContext)(null);
|
|
1781
1778
|
const AuthProvider2 = ({ children }) => {
|
|
1779
|
+
const storageType = options?.storage || "cookie";
|
|
1780
|
+
(0, import_react.useEffect)(() => {
|
|
1781
|
+
configureTokenManager({ storage: storageType });
|
|
1782
|
+
}, [storageType]);
|
|
1783
|
+
const manager = useTokenManager();
|
|
1782
1784
|
const [loading, setLoading] = (0, import_react.useState)(true);
|
|
1783
|
-
const
|
|
1785
|
+
const rawUser = useAuthStore((state) => state.user);
|
|
1784
1786
|
const error = useAuthStore((state) => state.error);
|
|
1787
|
+
const user = rawUser;
|
|
1785
1788
|
(0, import_react.useEffect)(() => {
|
|
1786
1789
|
const savedUser = manager.getSingleToken("user");
|
|
1787
1790
|
if (savedUser) {
|
|
@@ -1789,13 +1792,15 @@ function createAuthContext(options) {
|
|
|
1789
1792
|
const parsedUser = JSON.parse(savedUser);
|
|
1790
1793
|
useAuthStore.getState().setUser(parsedUser);
|
|
1791
1794
|
useAuthStore.getState().setError(null);
|
|
1792
|
-
} catch
|
|
1795
|
+
} catch {
|
|
1793
1796
|
useAuthStore.getState().resetAuth();
|
|
1794
|
-
useAuthStore.getState().setError(
|
|
1797
|
+
useAuthStore.getState().setError(
|
|
1798
|
+
new Error("Failed to parse saved user")
|
|
1799
|
+
);
|
|
1795
1800
|
}
|
|
1796
1801
|
}
|
|
1797
1802
|
setLoading(false);
|
|
1798
|
-
}, []);
|
|
1803
|
+
}, [manager]);
|
|
1799
1804
|
const setUser = (userData) => {
|
|
1800
1805
|
useAuthStore.getState().setUser(userData);
|
|
1801
1806
|
useAuthStore.getState().setError(null);
|
|
@@ -1806,7 +1811,9 @@ function createAuthContext(options) {
|
|
|
1806
1811
|
manager.setTokens(tokens);
|
|
1807
1812
|
setUser(userData);
|
|
1808
1813
|
} catch (err) {
|
|
1809
|
-
useAuthStore.getState().setError(
|
|
1814
|
+
useAuthStore.getState().setError(
|
|
1815
|
+
err instanceof Error ? err : new Error(String(err))
|
|
1816
|
+
);
|
|
1810
1817
|
}
|
|
1811
1818
|
};
|
|
1812
1819
|
const logout = () => {
|
|
@@ -1814,14 +1821,24 @@ function createAuthContext(options) {
|
|
|
1814
1821
|
manager.clearTokens();
|
|
1815
1822
|
useAuthStore.getState().resetAuth();
|
|
1816
1823
|
} catch (err) {
|
|
1817
|
-
useAuthStore.getState().setError(
|
|
1824
|
+
useAuthStore.getState().setError(
|
|
1825
|
+
err instanceof Error ? err : new Error(String(err))
|
|
1826
|
+
);
|
|
1818
1827
|
}
|
|
1819
1828
|
};
|
|
1820
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1829
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
1830
|
+
AuthContext.Provider,
|
|
1831
|
+
{
|
|
1832
|
+
value: { user, login, logout, setUser, loading, error },
|
|
1833
|
+
children
|
|
1834
|
+
}
|
|
1835
|
+
);
|
|
1821
1836
|
};
|
|
1822
1837
|
const useAuth2 = () => {
|
|
1823
1838
|
const ctx = (0, import_react.useContext)(AuthContext);
|
|
1824
|
-
if (!ctx)
|
|
1839
|
+
if (!ctx) {
|
|
1840
|
+
throw new Error("useAuth must be used inside AuthProvider");
|
|
1841
|
+
}
|
|
1825
1842
|
return ctx;
|
|
1826
1843
|
};
|
|
1827
1844
|
return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
|
package/package.json
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -11,7 +11,7 @@ import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
|
11
11
|
import { useAuthStore } from '../store/useGuardStore'
|
|
12
12
|
|
|
13
13
|
export type AuthContextType<UserType> = {
|
|
14
|
-
user:
|
|
14
|
+
user: UserType | null
|
|
15
15
|
login: (tokens: Record<string, string>, user: UserType) => void
|
|
16
16
|
logout: () => void
|
|
17
17
|
setUser: (user: UserType) => void
|
|
@@ -26,26 +26,26 @@ export type AuthContextOptions = {
|
|
|
26
26
|
/**
|
|
27
27
|
* Factory to create typed AuthProvider and useAuth hook
|
|
28
28
|
*/
|
|
29
|
-
export function createAuthContext<UserType>(
|
|
30
|
-
|
|
31
|
-
) {
|
|
32
|
-
const storageType = options?.storage || 'cookie'
|
|
29
|
+
export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
30
|
+
const AuthContext = createContext<AuthContextType<UserType> | null>(null)
|
|
33
31
|
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
33
|
+
const storageType = options?.storage || 'cookie'
|
|
36
34
|
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
// ✅ Configure token manager inside the component lifecycle
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
configureTokenManager({ storage: storageType })
|
|
38
|
+
}, [storageType])
|
|
39
39
|
|
|
40
|
-
const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
41
|
-
// ✅ Hook is now inside component (VALID)
|
|
42
40
|
const manager = useTokenManager()
|
|
43
|
-
|
|
44
41
|
const [loading, setLoading] = useState(true)
|
|
45
42
|
|
|
46
|
-
|
|
43
|
+
// Zustand state
|
|
44
|
+
const rawUser = useAuthStore((state) => state.user)
|
|
47
45
|
const error = useAuthStore((state) => state.error)
|
|
48
46
|
|
|
47
|
+
const user = rawUser as UserType | null
|
|
48
|
+
|
|
49
49
|
// Restore user on mount
|
|
50
50
|
useEffect(() => {
|
|
51
51
|
const savedUser = manager.getSingleToken('user')
|
|
@@ -80,11 +80,9 @@ export function createAuthContext<UserType>(
|
|
|
80
80
|
manager.setTokens(tokens)
|
|
81
81
|
setUser(userData)
|
|
82
82
|
} catch (err) {
|
|
83
|
-
useAuthStore
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
err instanceof Error ? err : new Error(String(err))
|
|
87
|
-
)
|
|
83
|
+
useAuthStore.getState().setError(
|
|
84
|
+
err instanceof Error ? err : new Error(String(err))
|
|
85
|
+
)
|
|
88
86
|
}
|
|
89
87
|
}
|
|
90
88
|
|
|
@@ -93,11 +91,9 @@ export function createAuthContext<UserType>(
|
|
|
93
91
|
manager.clearTokens()
|
|
94
92
|
useAuthStore.getState().resetAuth()
|
|
95
93
|
} catch (err) {
|
|
96
|
-
useAuthStore
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
err instanceof Error ? err : new Error(String(err))
|
|
100
|
-
)
|
|
94
|
+
useAuthStore.getState().setError(
|
|
95
|
+
err instanceof Error ? err : new Error(String(err))
|
|
96
|
+
)
|
|
101
97
|
}
|
|
102
98
|
}
|
|
103
99
|
|
|
@@ -113,9 +109,7 @@ export function createAuthContext<UserType>(
|
|
|
113
109
|
const useAuth = (): AuthContextType<UserType> => {
|
|
114
110
|
const ctx = useContext(AuthContext)
|
|
115
111
|
if (!ctx) {
|
|
116
|
-
throw new Error(
|
|
117
|
-
'useAuth must be used inside AuthProvider'
|
|
118
|
-
)
|
|
112
|
+
throw new Error('useAuth must be used inside AuthProvider')
|
|
119
113
|
}
|
|
120
114
|
return ctx
|
|
121
115
|
}
|