nextauthz 1.0.5 → 1.0.6
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 +1 -3
- package/dist/index.mjs +1 -3
- package/package.json +1 -1
- package/src/AuthProvider.tsx +7 -9
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
|
|
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
|
|
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
|
@@ -1852,9 +1852,7 @@ 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
1858
|
return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
|
package/dist/index.mjs
CHANGED
|
@@ -1836,9 +1836,7 @@ 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
1842
|
return { AuthProvider: AuthProvider2, useAuth: useAuth2 };
|
package/package.json
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -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
|
|
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
|
-
//
|
|
35
|
+
// Configure token manager once on mount
|
|
36
36
|
useEffect(() => {
|
|
37
37
|
configureTokenManager({ storage: storageType })
|
|
38
38
|
}, [storageType])
|
|
39
39
|
|
|
40
|
-
|
|
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
|
|
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
|
|