nextauthz 1.3.21 → 1.3.23
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.js +13 -6
- package/dist/index.mjs +13 -6
- package/package.json +1 -1
- package/src/AuthGuard.tsx +5 -1
- package/src/AuthProvider.tsx +2 -3
- package/src/RoleGuard.tsx +7 -2
- package/store/useGuardStore.ts +3 -3
package/dist/index.js
CHANGED
|
@@ -46,11 +46,12 @@ var useAuthStore = (0, import_zustand.create)((set) => ({
|
|
|
46
46
|
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
47
47
|
setLoading: (loading) => set({ loading }),
|
|
48
48
|
setError: (err) => set({ error: err }),
|
|
49
|
-
resetAuth: () => set({
|
|
49
|
+
resetAuth: (logout = false) => set({
|
|
50
50
|
user: null,
|
|
51
51
|
role: null,
|
|
52
52
|
isAuthenticated: false,
|
|
53
|
-
isAuthChecked: false,
|
|
53
|
+
isAuthChecked: logout ? true : false,
|
|
54
|
+
// ✅ true if logout, false if initial load failed
|
|
54
55
|
loading: false,
|
|
55
56
|
error: null
|
|
56
57
|
})
|
|
@@ -108,11 +109,10 @@ function createAuthContext(option) {
|
|
|
108
109
|
if (userData) setUser(userData);
|
|
109
110
|
setRole(role2 ?? null);
|
|
110
111
|
setAuth(true);
|
|
112
|
+
setAuthChecked(true);
|
|
111
113
|
};
|
|
112
114
|
const logout = () => {
|
|
113
115
|
manager.clearTokens();
|
|
114
|
-
setAuth(false);
|
|
115
|
-
setAuthChecked(true);
|
|
116
116
|
resetAuth();
|
|
117
117
|
};
|
|
118
118
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
@@ -162,7 +162,10 @@ var AuthGuard = ({
|
|
|
162
162
|
}
|
|
163
163
|
}, [isAuthenticated, isAuthChecked, redirectTo, router]);
|
|
164
164
|
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: fallback });
|
|
165
|
-
if (!isAuthenticated)
|
|
165
|
+
if (!isAuthenticated) {
|
|
166
|
+
router.replace(redirectTo);
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
166
169
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
167
170
|
};
|
|
168
171
|
var AuthGuard_default = AuthGuard;
|
|
@@ -178,7 +181,7 @@ var RoleGuard = ({
|
|
|
178
181
|
fallback = null
|
|
179
182
|
}) => {
|
|
180
183
|
const router = (0, import_navigation2.useRouter)();
|
|
181
|
-
const { role, isAuthChecked } = useAuthStore();
|
|
184
|
+
const { role, isAuthChecked, isAuthenticated } = useAuthStore();
|
|
182
185
|
(0, import_react3.useEffect)(() => {
|
|
183
186
|
if (!isAuthChecked) return;
|
|
184
187
|
if (!role || !allowedRoles.includes(role)) {
|
|
@@ -186,6 +189,10 @@ var RoleGuard = ({
|
|
|
186
189
|
}
|
|
187
190
|
}, [role, isAuthChecked, allowedRoles, redirectTo, router]);
|
|
188
191
|
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
|
|
192
|
+
if (!isAuthenticated) {
|
|
193
|
+
router.replace(redirectTo);
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
189
196
|
if (!role || !allowedRoles.includes(role)) return null;
|
|
190
197
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
191
198
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -19,11 +19,12 @@ var useAuthStore = create((set) => ({
|
|
|
19
19
|
setAuthChecked: (checked) => set({ isAuthChecked: checked }),
|
|
20
20
|
setLoading: (loading) => set({ loading }),
|
|
21
21
|
setError: (err) => set({ error: err }),
|
|
22
|
-
resetAuth: () => set({
|
|
22
|
+
resetAuth: (logout = false) => set({
|
|
23
23
|
user: null,
|
|
24
24
|
role: null,
|
|
25
25
|
isAuthenticated: false,
|
|
26
|
-
isAuthChecked: false,
|
|
26
|
+
isAuthChecked: logout ? true : false,
|
|
27
|
+
// ✅ true if logout, false if initial load failed
|
|
27
28
|
loading: false,
|
|
28
29
|
error: null
|
|
29
30
|
})
|
|
@@ -81,11 +82,10 @@ function createAuthContext(option) {
|
|
|
81
82
|
if (userData) setUser(userData);
|
|
82
83
|
setRole(role2 ?? null);
|
|
83
84
|
setAuth(true);
|
|
85
|
+
setAuthChecked(true);
|
|
84
86
|
};
|
|
85
87
|
const logout = () => {
|
|
86
88
|
manager.clearTokens();
|
|
87
|
-
setAuth(false);
|
|
88
|
-
setAuthChecked(true);
|
|
89
89
|
resetAuth();
|
|
90
90
|
};
|
|
91
91
|
return /* @__PURE__ */ jsx(
|
|
@@ -135,7 +135,10 @@ var AuthGuard = ({
|
|
|
135
135
|
}
|
|
136
136
|
}, [isAuthenticated, isAuthChecked, redirectTo, router]);
|
|
137
137
|
if (!isAuthChecked) return /* @__PURE__ */ jsx2(Fragment, { children: fallback });
|
|
138
|
-
if (!isAuthenticated)
|
|
138
|
+
if (!isAuthenticated) {
|
|
139
|
+
router.replace(redirectTo);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
139
142
|
return /* @__PURE__ */ jsx2(Fragment, { children });
|
|
140
143
|
};
|
|
141
144
|
var AuthGuard_default = AuthGuard;
|
|
@@ -151,7 +154,7 @@ var RoleGuard = ({
|
|
|
151
154
|
fallback = null
|
|
152
155
|
}) => {
|
|
153
156
|
const router = useRouter2();
|
|
154
|
-
const { role, isAuthChecked } = useAuthStore();
|
|
157
|
+
const { role, isAuthChecked, isAuthenticated } = useAuthStore();
|
|
155
158
|
useEffect3(() => {
|
|
156
159
|
if (!isAuthChecked) return;
|
|
157
160
|
if (!role || !allowedRoles.includes(role)) {
|
|
@@ -159,6 +162,10 @@ var RoleGuard = ({
|
|
|
159
162
|
}
|
|
160
163
|
}, [role, isAuthChecked, allowedRoles, redirectTo, router]);
|
|
161
164
|
if (!isAuthChecked) return /* @__PURE__ */ jsx3(Fragment2, { children: fallback });
|
|
165
|
+
if (!isAuthenticated) {
|
|
166
|
+
router.replace(redirectTo);
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
162
169
|
if (!role || !allowedRoles.includes(role)) return null;
|
|
163
170
|
return /* @__PURE__ */ jsx3(Fragment2, { children });
|
|
164
171
|
};
|
package/package.json
CHANGED
package/src/AuthGuard.tsx
CHANGED
package/src/AuthProvider.tsx
CHANGED
|
@@ -95,7 +95,8 @@ export function createAuthContext<UserType extends User = User>(option?: {
|
|
|
95
95
|
|
|
96
96
|
if (userData) setUser(userData)
|
|
97
97
|
setRole(role ?? null)
|
|
98
|
-
setAuth(true)
|
|
98
|
+
setAuth(true);
|
|
99
|
+
setAuthChecked(true);
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
/* ---------------------------------- */
|
|
@@ -104,8 +105,6 @@ export function createAuthContext<UserType extends User = User>(option?: {
|
|
|
104
105
|
|
|
105
106
|
const logout = () => {
|
|
106
107
|
manager.clearTokens()
|
|
107
|
-
setAuth(false);
|
|
108
|
-
setAuthChecked(true); // mark that auth is now checked
|
|
109
108
|
resetAuth()
|
|
110
109
|
}
|
|
111
110
|
|
package/src/RoleGuard.tsx
CHANGED
|
@@ -18,7 +18,7 @@ const RoleGuard = ({
|
|
|
18
18
|
fallback = null,
|
|
19
19
|
}: RoleGuardProps) => {
|
|
20
20
|
const router = useRouter()
|
|
21
|
-
const { role, isAuthChecked } = useAuthStore()
|
|
21
|
+
const { role, isAuthChecked, isAuthenticated } = useAuthStore()
|
|
22
22
|
|
|
23
23
|
useEffect(() => {
|
|
24
24
|
if (!isAuthChecked) return
|
|
@@ -29,9 +29,14 @@ const RoleGuard = ({
|
|
|
29
29
|
}
|
|
30
30
|
}, [role, isAuthChecked, allowedRoles, redirectTo, router])
|
|
31
31
|
|
|
32
|
-
// Show fallback while loading
|
|
33
32
|
if (!isAuthChecked) return <>{fallback}</>
|
|
34
33
|
|
|
34
|
+
if (!isAuthenticated) {
|
|
35
|
+
router.replace(redirectTo)
|
|
36
|
+
return null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
35
40
|
// Block rendering if role is not allowed
|
|
36
41
|
if (!role || !allowedRoles.includes(role)) return null
|
|
37
42
|
|
package/store/useGuardStore.ts
CHANGED
|
@@ -41,13 +41,13 @@ export const useAuthStore = create<AuthState>((set) => ({
|
|
|
41
41
|
setLoading: (loading) => set({ loading }),
|
|
42
42
|
setError: (err) => set({ error: err }),
|
|
43
43
|
|
|
44
|
-
resetAuth: () =>
|
|
44
|
+
resetAuth: (logout = false) =>
|
|
45
45
|
set({
|
|
46
46
|
user: null,
|
|
47
47
|
role: null,
|
|
48
48
|
isAuthenticated: false,
|
|
49
|
-
isAuthChecked: false,
|
|
49
|
+
isAuthChecked: logout ? true : false, // ✅ true if logout, false if initial load failed
|
|
50
50
|
loading: false,
|
|
51
51
|
error: null,
|
|
52
|
-
}),
|
|
52
|
+
}),
|
|
53
53
|
}))
|