Implement activation requirement for protected routes
- Added a new `useRequireActivated` hook to enforce user activation before accessing certain routes, redirecting unauthenticated users to the login page and inactive users to the dashboard. - Updated the `InstructionsPage` and `NewsPage` components to utilize the new activation check, enhancing user flow and security. - Conditional rendering of navigation links in the `RootLayout` based on user activation status, improving user experience by hiding inaccessible features.
This commit is contained in:
@@ -37,3 +37,17 @@ export function useRequireAdmin() {
|
||||
|
||||
return { user, isReady: !isBootstrapping && !!user && user.role === 'admin' }
|
||||
}
|
||||
|
||||
/** Как useRequireAuth, но дополнительно требует активацию — иначе редирект на дашборд (там форма запроса активации). */
|
||||
export function useRequireActivated() {
|
||||
const { user, isBootstrapping } = useAuthStore()
|
||||
const navigate = useNavigate()
|
||||
|
||||
useEffect(() => {
|
||||
if (isBootstrapping) return
|
||||
if (!user) void navigate({ to: '/login' })
|
||||
else if (!user.isActivated) void navigate({ to: '/dashboard' })
|
||||
}, [isBootstrapping, user, navigate])
|
||||
|
||||
return { user, isReady: !isBootstrapping && !!user && user.isActivated }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user