nextauthz 1.3.21 → 1.3.22
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 +12 -6
- package/dist/index.mjs +12 -6
- package/package.json +1 -1
- package/src/AuthGuard.tsx +5 -1
- package/src/AuthProvider.tsx +0 -2
- 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
|
})
|
|
@@ -111,8 +112,6 @@ function createAuthContext(option) {
|
|
|
111
112
|
};
|
|
112
113
|
const logout = () => {
|
|
113
114
|
manager.clearTokens();
|
|
114
|
-
setAuth(false);
|
|
115
|
-
setAuthChecked(true);
|
|
116
115
|
resetAuth();
|
|
117
116
|
};
|
|
118
117
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
@@ -162,7 +161,10 @@ var AuthGuard = ({
|
|
|
162
161
|
}
|
|
163
162
|
}, [isAuthenticated, isAuthChecked, redirectTo, router]);
|
|
164
163
|
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: fallback });
|
|
165
|
-
if (!isAuthenticated)
|
|
164
|
+
if (!isAuthenticated) {
|
|
165
|
+
router.replace(redirectTo);
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
166
168
|
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children });
|
|
167
169
|
};
|
|
168
170
|
var AuthGuard_default = AuthGuard;
|
|
@@ -178,7 +180,7 @@ var RoleGuard = ({
|
|
|
178
180
|
fallback = null
|
|
179
181
|
}) => {
|
|
180
182
|
const router = (0, import_navigation2.useRouter)();
|
|
181
|
-
const { role, isAuthChecked } = useAuthStore();
|
|
183
|
+
const { role, isAuthChecked, isAuthenticated } = useAuthStore();
|
|
182
184
|
(0, import_react3.useEffect)(() => {
|
|
183
185
|
if (!isAuthChecked) return;
|
|
184
186
|
if (!role || !allowedRoles.includes(role)) {
|
|
@@ -186,6 +188,10 @@ var RoleGuard = ({
|
|
|
186
188
|
}
|
|
187
189
|
}, [role, isAuthChecked, allowedRoles, redirectTo, router]);
|
|
188
190
|
if (!isAuthChecked) return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children: fallback });
|
|
191
|
+
if (!isAuthenticated) {
|
|
192
|
+
router.replace(redirectTo);
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
189
195
|
if (!role || !allowedRoles.includes(role)) return null;
|
|
190
196
|
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children });
|
|
191
197
|
};
|
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
|
})
|
|
@@ -84,8 +85,6 @@ function createAuthContext(option) {
|
|
|
84
85
|
};
|
|
85
86
|
const logout = () => {
|
|
86
87
|
manager.clearTokens();
|
|
87
|
-
setAuth(false);
|
|
88
|
-
setAuthChecked(true);
|
|
89
88
|
resetAuth();
|
|
90
89
|
};
|
|
91
90
|
return /* @__PURE__ */ jsx(
|
|
@@ -135,7 +134,10 @@ var AuthGuard = ({
|
|
|
135
134
|
}
|
|
136
135
|
}, [isAuthenticated, isAuthChecked, redirectTo, router]);
|
|
137
136
|
if (!isAuthChecked) return /* @__PURE__ */ jsx2(Fragment, { children: fallback });
|
|
138
|
-
if (!isAuthenticated)
|
|
137
|
+
if (!isAuthenticated) {
|
|
138
|
+
router.replace(redirectTo);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
139
141
|
return /* @__PURE__ */ jsx2(Fragment, { children });
|
|
140
142
|
};
|
|
141
143
|
var AuthGuard_default = AuthGuard;
|
|
@@ -151,7 +153,7 @@ var RoleGuard = ({
|
|
|
151
153
|
fallback = null
|
|
152
154
|
}) => {
|
|
153
155
|
const router = useRouter2();
|
|
154
|
-
const { role, isAuthChecked } = useAuthStore();
|
|
156
|
+
const { role, isAuthChecked, isAuthenticated } = useAuthStore();
|
|
155
157
|
useEffect3(() => {
|
|
156
158
|
if (!isAuthChecked) return;
|
|
157
159
|
if (!role || !allowedRoles.includes(role)) {
|
|
@@ -159,6 +161,10 @@ var RoleGuard = ({
|
|
|
159
161
|
}
|
|
160
162
|
}, [role, isAuthChecked, allowedRoles, redirectTo, router]);
|
|
161
163
|
if (!isAuthChecked) return /* @__PURE__ */ jsx3(Fragment2, { children: fallback });
|
|
164
|
+
if (!isAuthenticated) {
|
|
165
|
+
router.replace(redirectTo);
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
162
168
|
if (!role || !allowedRoles.includes(role)) return null;
|
|
163
169
|
return /* @__PURE__ */ jsx3(Fragment2, { children });
|
|
164
170
|
};
|
package/package.json
CHANGED
package/src/AuthGuard.tsx
CHANGED
package/src/AuthProvider.tsx
CHANGED
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
|
}))
|