nextauthz 1.0.1 → 1.0.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/package.json +1 -1
- package/src/AuthProvider.tsx +52 -17
package/package.json
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
'use client'
|
|
2
2
|
|
|
3
|
-
import React, {
|
|
3
|
+
import React, {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
ReactNode,
|
|
7
|
+
useState,
|
|
8
|
+
useEffect,
|
|
9
|
+
} from 'react'
|
|
4
10
|
import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
5
11
|
import { useAuthStore } from '../store/useGuardStore'
|
|
6
12
|
|
|
7
13
|
export type AuthContextType<UserType> = {
|
|
8
|
-
user:
|
|
14
|
+
user: any
|
|
9
15
|
login: (tokens: Record<string, string>, user: UserType) => void
|
|
10
16
|
logout: () => void
|
|
11
17
|
setUser: (user: UserType) => void
|
|
@@ -20,46 +26,65 @@ export type AuthContextOptions = {
|
|
|
20
26
|
/**
|
|
21
27
|
* Factory to create typed AuthProvider and useAuth hook
|
|
22
28
|
*/
|
|
23
|
-
export function createAuthContext<UserType>(
|
|
29
|
+
export function createAuthContext<UserType>(
|
|
30
|
+
options?: AuthContextOptions
|
|
31
|
+
) {
|
|
24
32
|
const storageType = options?.storage || 'cookie'
|
|
33
|
+
|
|
34
|
+
// ✅ SAFE — not a hook
|
|
25
35
|
configureTokenManager({ storage: storageType })
|
|
26
|
-
const manager = useTokenManager()
|
|
27
36
|
|
|
28
|
-
const AuthContext =
|
|
37
|
+
const AuthContext =
|
|
38
|
+
createContext<AuthContextType<UserType> | null>(null)
|
|
29
39
|
|
|
30
40
|
const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
41
|
+
// ✅ Hook is now inside component (VALID)
|
|
42
|
+
const manager = useTokenManager()
|
|
43
|
+
|
|
31
44
|
const [loading, setLoading] = useState(true)
|
|
45
|
+
|
|
32
46
|
const user = useAuthStore((state) => state.user)
|
|
33
47
|
const error = useAuthStore((state) => state.error)
|
|
34
48
|
|
|
35
49
|
// Restore user on mount
|
|
36
50
|
useEffect(() => {
|
|
37
51
|
const savedUser = manager.getSingleToken('user')
|
|
52
|
+
|
|
38
53
|
if (savedUser) {
|
|
39
54
|
try {
|
|
40
55
|
const parsedUser = JSON.parse(savedUser)
|
|
41
56
|
useAuthStore.getState().setUser(parsedUser)
|
|
42
57
|
useAuthStore.getState().setError(null)
|
|
43
|
-
} catch
|
|
58
|
+
} catch {
|
|
44
59
|
useAuthStore.getState().resetAuth()
|
|
45
|
-
useAuthStore.getState().setError(
|
|
60
|
+
useAuthStore.getState().setError(
|
|
61
|
+
new Error('Failed to parse saved user')
|
|
62
|
+
)
|
|
46
63
|
}
|
|
47
64
|
}
|
|
65
|
+
|
|
48
66
|
setLoading(false)
|
|
49
|
-
}, [])
|
|
67
|
+
}, [manager])
|
|
50
68
|
|
|
51
|
-
const setUser = (userData:
|
|
52
|
-
useAuthStore.getState().setUser(userData)
|
|
69
|
+
const setUser = (userData: UserType) => {
|
|
70
|
+
useAuthStore.getState().setUser(userData as any)
|
|
53
71
|
useAuthStore.getState().setError(null)
|
|
54
72
|
manager.setTokens({ user: JSON.stringify(userData) })
|
|
55
73
|
}
|
|
56
74
|
|
|
57
|
-
const login = (
|
|
75
|
+
const login = (
|
|
76
|
+
tokens: Record<string, string>,
|
|
77
|
+
userData: UserType
|
|
78
|
+
) => {
|
|
58
79
|
try {
|
|
59
80
|
manager.setTokens(tokens)
|
|
60
81
|
setUser(userData)
|
|
61
|
-
} catch (err
|
|
62
|
-
useAuthStore
|
|
82
|
+
} catch (err) {
|
|
83
|
+
useAuthStore
|
|
84
|
+
.getState()
|
|
85
|
+
.setError(
|
|
86
|
+
err instanceof Error ? err : new Error(String(err))
|
|
87
|
+
)
|
|
63
88
|
}
|
|
64
89
|
}
|
|
65
90
|
|
|
@@ -67,13 +92,19 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
67
92
|
try {
|
|
68
93
|
manager.clearTokens()
|
|
69
94
|
useAuthStore.getState().resetAuth()
|
|
70
|
-
} catch (err
|
|
71
|
-
useAuthStore
|
|
95
|
+
} catch (err) {
|
|
96
|
+
useAuthStore
|
|
97
|
+
.getState()
|
|
98
|
+
.setError(
|
|
99
|
+
err instanceof Error ? err : new Error(String(err))
|
|
100
|
+
)
|
|
72
101
|
}
|
|
73
102
|
}
|
|
74
103
|
|
|
75
104
|
return (
|
|
76
|
-
<AuthContext.Provider
|
|
105
|
+
<AuthContext.Provider
|
|
106
|
+
value={{ user, login, logout, setUser, loading, error }}
|
|
107
|
+
>
|
|
77
108
|
{children}
|
|
78
109
|
</AuthContext.Provider>
|
|
79
110
|
)
|
|
@@ -81,7 +112,11 @@ export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
|
81
112
|
|
|
82
113
|
const useAuth = (): AuthContextType<UserType> => {
|
|
83
114
|
const ctx = useContext(AuthContext)
|
|
84
|
-
if (!ctx)
|
|
115
|
+
if (!ctx) {
|
|
116
|
+
throw new Error(
|
|
117
|
+
'useAuth must be used inside AuthProvider'
|
|
118
|
+
)
|
|
119
|
+
}
|
|
85
120
|
return ctx
|
|
86
121
|
}
|
|
87
122
|
|