mirror of https://github.com/portainer/portainer
fix(auth): isAdmin redirect for wizard [EE-6669] (#11075)
Co-authored-by: testa113 <testa113>pull/11103/head
parent
ce4b0e759c
commit
08dd7f6d2a
@ -0,0 +1,28 @@
|
||||
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]);
|
||||
}
|
Loading…
Reference in new issue