nextauthz 1.0.4 → 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 +8 -15
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,24 +32,23 @@ 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
|
+
// ✅ Hooks must be called inside component
|
|
40
41
|
const manager = useTokenManager()
|
|
42
|
+
|
|
41
43
|
const [loading, setLoading] = useState(true)
|
|
42
44
|
|
|
43
|
-
// Zustand state
|
|
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
|
-
|
|
53
52
|
if (savedUser) {
|
|
54
53
|
try {
|
|
55
54
|
const parsedUser = JSON.parse(savedUser)
|
|
@@ -62,7 +61,6 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
62
61
|
)
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
|
-
|
|
66
64
|
setLoading(false)
|
|
67
65
|
}, [manager])
|
|
68
66
|
|
|
@@ -72,10 +70,7 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
72
70
|
manager.setTokens({ user: JSON.stringify(userData) })
|
|
73
71
|
}
|
|
74
72
|
|
|
75
|
-
const login = (
|
|
76
|
-
tokens: Record<string, string>,
|
|
77
|
-
userData: UserType
|
|
78
|
-
) => {
|
|
73
|
+
const login = (tokens: Record<string, string>, userData: UserType) => {
|
|
79
74
|
try {
|
|
80
75
|
manager.setTokens(tokens)
|
|
81
76
|
setUser(userData)
|
|
@@ -108,9 +103,7 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
108
103
|
|
|
109
104
|
const useAuth = (): AuthContextType<UserType> => {
|
|
110
105
|
const ctx = useContext(AuthContext)
|
|
111
|
-
if (!ctx)
|
|
112
|
-
throw new Error('useAuth must be used inside AuthProvider')
|
|
113
|
-
}
|
|
106
|
+
if (!ctx) throw new Error('useAuth must be used inside AuthProvider')
|
|
114
107
|
return ctx
|
|
115
108
|
}
|
|
116
109
|
|