nextauthz 1.1.6 → 1.1.8
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 +37 -4
- package/dist/index.d.ts +37 -4
- package/dist/index.js +119 -1807
- package/dist/index.mjs +125 -1823
- package/package.json +13 -9
- package/src/AuthProvider.tsx +109 -135
- package/src/RoleGuard.tsx +40 -40
- package/src/index.ts +8 -8
- package/src/myAuth.ts +4 -4
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nextauthz",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
10
|
-
"dev": "tsup src/index.ts --watch"
|
|
9
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --external react --external react-dom --external zustand --external next",
|
|
10
|
+
"dev": "tsup src/index.ts --watch --external react --external react-dom --external zustand --external next"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [],
|
|
13
13
|
"author": "",
|
|
@@ -15,12 +15,16 @@
|
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@types/react": "^19.2.14",
|
|
17
17
|
"@types/react-dom": "^19.2.3",
|
|
18
|
-
"typescript": "^5.9.3"
|
|
18
|
+
"typescript": "^5.9.3",
|
|
19
|
+
"tsup": "^8.5.1"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"react": "^18.2.0 || ^19.0.0",
|
|
23
|
+
"react-dom": "^18.2.0 || ^19.0.0",
|
|
24
|
+
"next": "^16.0.0",
|
|
25
|
+
"zustand": "^5.0.0"
|
|
19
26
|
},
|
|
20
27
|
"dependencies": {
|
|
21
|
-
"
|
|
22
|
-
"react-token-manager": "^1.0.7",
|
|
23
|
-
"tsup": "^8.5.1",
|
|
24
|
-
"zustand": "^5.0.11"
|
|
28
|
+
"react-token-manager": "^1.0.7"
|
|
25
29
|
}
|
|
26
|
-
}
|
|
30
|
+
}
|
package/src/AuthProvider.tsx
CHANGED
|
@@ -1,137 +1,111 @@
|
|
|
1
|
-
// 'use client'
|
|
2
|
-
|
|
3
|
-
// import React, {
|
|
4
|
-
// createContext,
|
|
5
|
-
// useContext,
|
|
6
|
-
// ReactNode,
|
|
7
|
-
// useEffect,
|
|
8
|
-
// useState,
|
|
9
|
-
// } from 'react'
|
|
10
|
-
// import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
11
|
-
// import { useAuthStore } from '../store/useGuardStore'
|
|
12
|
-
|
|
13
|
-
// export type AuthContextType<UserType> = {
|
|
14
|
-
// user: UserType | null
|
|
15
|
-
// login: (tokens: Record<string, string>, user: UserType) => void
|
|
16
|
-
// logout: () => void
|
|
17
|
-
// setUser: (user: UserType) => void
|
|
18
|
-
// loading: boolean
|
|
19
|
-
// error: Error | null
|
|
20
|
-
// }
|
|
21
|
-
|
|
22
|
-
// export type AuthContextOptions = {
|
|
23
|
-
// storage?: 'localStorage' | 'sessionStorage' | 'cookie'
|
|
24
|
-
// }
|
|
25
|
-
|
|
26
|
-
// /**
|
|
27
|
-
// * Factory to create AuthProvider and typed useAuth hook
|
|
28
|
-
// */
|
|
29
|
-
// export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
30
|
-
// const AuthContext = createContext<AuthContextType<UserType> | null>(null)
|
|
31
|
-
|
|
32
|
-
// const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
33
|
-
// const storageType = options?.storage || 'cookie'
|
|
34
|
-
|
|
35
|
-
// // Configure token manager once on mount
|
|
36
|
-
// useEffect(() => {
|
|
37
|
-
// configureTokenManager({ storage: storageType })
|
|
38
|
-
// }, [storageType])
|
|
39
|
-
|
|
40
|
-
// // ✅ Hooks must be called inside component
|
|
41
|
-
// const manager = useTokenManager()
|
|
42
|
-
|
|
43
|
-
// const [loading, setLoading] = useState(true)
|
|
44
|
-
|
|
45
|
-
// const rawUser = useAuthStore((state) => state.user)
|
|
46
|
-
// const error = useAuthStore((state) => state.error)
|
|
47
|
-
// const user = rawUser as UserType | null
|
|
48
|
-
|
|
49
|
-
// // Restore saved user from token manager
|
|
50
|
-
// useEffect(() => {
|
|
51
|
-
// const savedUser = manager.getSingleToken('user')
|
|
52
|
-
// if (savedUser) {
|
|
53
|
-
// try {
|
|
54
|
-
// const parsedUser = JSON.parse(savedUser)
|
|
55
|
-
// useAuthStore.getState().setUser(parsedUser)
|
|
56
|
-
// useAuthStore.getState().setError(null)
|
|
57
|
-
// } catch {
|
|
58
|
-
// useAuthStore.getState().resetAuth()
|
|
59
|
-
// useAuthStore.getState().setError(
|
|
60
|
-
// new Error('Failed to parse saved user')
|
|
61
|
-
// )
|
|
62
|
-
// }
|
|
63
|
-
// }
|
|
64
|
-
// setLoading(false)
|
|
65
|
-
// }, [manager])
|
|
66
|
-
|
|
67
|
-
// const setUser = (userData: UserType) => {
|
|
68
|
-
// useAuthStore.getState().setUser(userData as any)
|
|
69
|
-
// useAuthStore.getState().setError(null)
|
|
70
|
-
// manager.setTokens({ user: JSON.stringify(userData) })
|
|
71
|
-
// }
|
|
72
|
-
|
|
73
|
-
// const login = (tokens: Record<string, string>, userData: UserType) => {
|
|
74
|
-
// try {
|
|
75
|
-
// manager.setTokens(tokens)
|
|
76
|
-
// setUser(userData)
|
|
77
|
-
// } catch (err) {
|
|
78
|
-
// useAuthStore.getState().setError(
|
|
79
|
-
// err instanceof Error ? err : new Error(String(err))
|
|
80
|
-
// )
|
|
81
|
-
// }
|
|
82
|
-
// }
|
|
83
|
-
|
|
84
|
-
// const logout = () => {
|
|
85
|
-
// try {
|
|
86
|
-
// manager.clearTokens()
|
|
87
|
-
// useAuthStore.getState().resetAuth()
|
|
88
|
-
// } catch (err) {
|
|
89
|
-
// useAuthStore.getState().setError(
|
|
90
|
-
// err instanceof Error ? err : new Error(String(err))
|
|
91
|
-
// )
|
|
92
|
-
// }
|
|
93
|
-
// }
|
|
94
|
-
|
|
95
|
-
// return (
|
|
96
|
-
// <AuthContext.Provider
|
|
97
|
-
// value={{ user, login, logout, setUser, loading, error }}
|
|
98
|
-
// >
|
|
99
|
-
// {children}
|
|
100
|
-
// </AuthContext.Provider>
|
|
101
|
-
// )
|
|
102
|
-
// }
|
|
103
|
-
|
|
104
|
-
// const useAuth = (): AuthContextType<UserType> => {
|
|
105
|
-
// const ctx = useContext(AuthContext)
|
|
106
|
-
// if (!ctx) throw new Error('useAuth must be used inside AuthProvider')
|
|
107
|
-
// return ctx
|
|
108
|
-
// }
|
|
109
|
-
|
|
110
|
-
// return { AuthProvider, useAuth }
|
|
111
|
-
// }
|
|
112
|
-
|
|
113
|
-
// src/context/ThemeContext.js
|
|
114
1
|
'use client'
|
|
115
|
-
import { createContext, useState } from 'react';
|
|
116
|
-
|
|
117
|
-
// 1️⃣ Create the context
|
|
118
|
-
export const ThemeContext = createContext({
|
|
119
|
-
theme: 'light', // default value
|
|
120
|
-
toggleTheme: () => {} // default empty function
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// 2️⃣ Create the provider component
|
|
124
|
-
export const ThemeProvider = ({ children }: any) => {
|
|
125
|
-
const [theme, setTheme] = useState('light');
|
|
126
|
-
|
|
127
|
-
const toggleTheme = () => {
|
|
128
|
-
setTheme(prev => (prev === 'light' ? 'dark' : 'light'));
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
return (
|
|
132
|
-
<ThemeContext.Provider value={{ theme, toggleTheme }}>
|
|
133
|
-
{children}
|
|
134
|
-
</ThemeContext.Provider>
|
|
135
|
-
);
|
|
136
|
-
};
|
|
137
2
|
|
|
3
|
+
import React, {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
ReactNode,
|
|
7
|
+
useEffect,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react'
|
|
10
|
+
import { configureTokenManager, useTokenManager } from 'react-token-manager'
|
|
11
|
+
import { useAuthStore } from '../store/useGuardStore'
|
|
12
|
+
|
|
13
|
+
export type AuthContextType<UserType> = {
|
|
14
|
+
user: UserType | null
|
|
15
|
+
login: (tokens: Record<string, string>, user: UserType) => void
|
|
16
|
+
logout: () => void
|
|
17
|
+
setUser: (user: UserType) => void
|
|
18
|
+
loading: boolean
|
|
19
|
+
error: Error | null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type AuthContextOptions = {
|
|
23
|
+
storage?: 'localStorage' | 'sessionStorage' | 'cookie'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Factory to create AuthProvider and typed useAuth hook
|
|
28
|
+
*/
|
|
29
|
+
export function createAuthContext<UserType>(options?: AuthContextOptions) {
|
|
30
|
+
const AuthContext = createContext<AuthContextType<UserType> | null>(null)
|
|
31
|
+
|
|
32
|
+
const AuthProvider = ({ children }: { children: ReactNode }) => {
|
|
33
|
+
const storageType = options?.storage || 'cookie'
|
|
34
|
+
|
|
35
|
+
// Configure token manager once on mount
|
|
36
|
+
useEffect(() => {
|
|
37
|
+
configureTokenManager({ storage: storageType })
|
|
38
|
+
}, [storageType])
|
|
39
|
+
|
|
40
|
+
// ✅ Hooks must be called inside component
|
|
41
|
+
const manager = useTokenManager()
|
|
42
|
+
|
|
43
|
+
const [loading, setLoading] = useState(true)
|
|
44
|
+
|
|
45
|
+
const rawUser = useAuthStore((state) => state.user)
|
|
46
|
+
const error = useAuthStore((state) => state.error)
|
|
47
|
+
const user = rawUser as UserType | null
|
|
48
|
+
|
|
49
|
+
// Restore saved user from token manager
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
const savedUser = manager.getSingleToken('user')
|
|
52
|
+
if (savedUser) {
|
|
53
|
+
try {
|
|
54
|
+
const parsedUser = JSON.parse(savedUser)
|
|
55
|
+
useAuthStore.getState().setUser(parsedUser)
|
|
56
|
+
useAuthStore.getState().setError(null)
|
|
57
|
+
} catch {
|
|
58
|
+
useAuthStore.getState().resetAuth()
|
|
59
|
+
useAuthStore.getState().setError(
|
|
60
|
+
new Error('Failed to parse saved user')
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
setLoading(false)
|
|
65
|
+
}, [manager])
|
|
66
|
+
|
|
67
|
+
const setUser = (userData: UserType) => {
|
|
68
|
+
useAuthStore.getState().setUser(userData as any)
|
|
69
|
+
useAuthStore.getState().setError(null)
|
|
70
|
+
manager.setTokens({ user: JSON.stringify(userData) })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const login = (tokens: Record<string, string>, userData: UserType) => {
|
|
74
|
+
try {
|
|
75
|
+
manager.setTokens(tokens)
|
|
76
|
+
setUser(userData)
|
|
77
|
+
} catch (err) {
|
|
78
|
+
useAuthStore.getState().setError(
|
|
79
|
+
err instanceof Error ? err : new Error(String(err))
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const logout = () => {
|
|
85
|
+
try {
|
|
86
|
+
manager.clearTokens()
|
|
87
|
+
useAuthStore.getState().resetAuth()
|
|
88
|
+
} catch (err) {
|
|
89
|
+
useAuthStore.getState().setError(
|
|
90
|
+
err instanceof Error ? err : new Error(String(err))
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<AuthContext.Provider
|
|
97
|
+
value={{ user, login, logout, setUser, loading, error }}
|
|
98
|
+
>
|
|
99
|
+
{children}
|
|
100
|
+
</AuthContext.Provider>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const useAuth = (): AuthContextType<UserType> => {
|
|
105
|
+
const ctx = useContext(AuthContext)
|
|
106
|
+
if (!ctx) throw new Error('useAuth must be used inside AuthProvider')
|
|
107
|
+
return ctx
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { AuthProvider, useAuth }
|
|
111
|
+
}
|
package/src/RoleGuard.tsx
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
import React, { useEffect, useState } from 'react'
|
|
4
|
+
import { useRouter } from 'next/navigation'
|
|
5
|
+
import { auth } from './myAuth'
|
|
6
|
+
|
|
7
|
+
type RoleGuardProps = {
|
|
8
|
+
children: React.ReactNode
|
|
9
|
+
allowedRoles: string[]
|
|
10
|
+
redirectTo?: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const RoleGuard: React.FC<RoleGuardProps> = ({
|
|
14
|
+
children,
|
|
15
|
+
allowedRoles,
|
|
16
|
+
redirectTo = '/unauthorized',
|
|
17
|
+
}) => {
|
|
18
|
+
// ✅ Destructure useAuth INSIDE the component
|
|
19
|
+
const { useAuth } = auth
|
|
20
|
+
const { user, loading } = useAuth()
|
|
21
|
+
|
|
22
|
+
const router = useRouter()
|
|
23
|
+
const [isChecking, setIsChecking] = useState(true)
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
if (!user) return
|
|
27
|
+
|
|
28
|
+
const hasAccess = allowedRoles.includes(user?.role)
|
|
29
|
+
if (!hasAccess) {
|
|
30
|
+
router.replace(redirectTo)
|
|
31
|
+
}
|
|
32
|
+
setIsChecking(false)
|
|
33
|
+
}, [user, allowedRoles, redirectTo, router])
|
|
34
|
+
|
|
35
|
+
if (loading || !user || isChecking) return <div>Loading...</div>
|
|
36
|
+
|
|
37
|
+
return <>{children}</>
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default RoleGuard
|
package/src/index.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createAuthContext } from './AuthProvider'
|
|
2
2
|
|
|
3
3
|
export type User = {
|
|
4
4
|
[key: string]: any
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
// Export factory instead of fixed instance
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
export function createAppAuth(
|
|
9
|
+
storage: 'localStorage' | 'sessionStorage' | 'cookie' = 'cookie'
|
|
10
|
+
) {
|
|
11
|
+
return createAuthContext<User>({ storage })
|
|
12
|
+
}
|
|
13
13
|
|
|
14
|
-
export {
|
|
14
|
+
export { createAuthContext } from './AuthProvider'
|
|
15
15
|
export { default as AuthGuard } from './AuthGuard'
|
|
16
|
-
|
|
16
|
+
export { default as RoleGuard } from './RoleGuard'
|
package/src/myAuth.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// myAuth.ts
|
|
2
|
+
'use client'
|
|
3
|
+
import { createAppAuth } from '.'
|
|
4
|
+
export const auth = createAppAuth() // { AuthProvider, useAuth }
|