# All files / src/hooks useBannedRedirect.ts

0% Statements 0/13
0% Branches 0/10
0% Functions 0/2
0% Lines 0/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28

     import { useEffect } from 'react';
    import { useRouter, usePathname } from 'next/navigation';
    import { useAuth } from '@/contexts/AuthContext';

    /**
     * Hook to automatically redirect banned users to the banned page
     * Should be used in protected pages to ensure banned users can't access them
     */
    export const useBannedRedirect = () => {
      const { user, isAuthenticated, isBanned } = useAuth();
      const router = useRouter();
      const pathname = usePathname();

      useEffect(() => {
        // Only check if user is authenticated and we're not already on the banned page
        if (isAuthenticated && user && pathname !== '/banned') {
          if (isBanned()) {
            router.push('/banned');
          }
        }
      }, [user, isAuthenticated, isBanned, router, pathname]);

      return {
        isBanned: isBanned(),
        shouldRedirect: isAuthenticated && isBanned() && pathname !== '/banned'
      };
    };