Files
backroad/app/react/hooks/useAdminOnlyRedirect.ts
Ali 08dd7f6d2a fix(auth): isAdmin redirect for wizard [EE-6669] (#11075)
Co-authored-by: testa113 <testa113>
2024-02-12 08:04:44 +13:00

29 lines
716 B
TypeScript

import { RawParams, useRouter } from '@uirouter/react';
import { useEffect } from 'react';
import { useCurrentUser } from './useUser';
type RedirectOptions = {
to: string;
params?: RawParams;
};
/**
* Redirects to the given route if the user is not a Portainer admin.
* @param to The route to redirect to (default is `'portainer.home'`).
* @param params The params to pass to the route.
*/
export function useAdminOnlyRedirect(
{ to, params }: RedirectOptions = { to: 'portainer.home' }
) {
const router = useRouter();
const { isAdmin } = useCurrentUser();
useEffect(() => {
if (!isAdmin) {
router.stateService.go(to, params);
}
}, [isAdmin, to, params, router.stateService]);
}