All files / features / auth AuthGuard.tsx

80.55% Statements 29/36
50% Branches 8/16
88.88% Functions 8/9
82.75% Lines 24/29

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      54x   54x   134x 397x 134x 134x 16x 16x 1x 1x          
import { type ReactNode } from 'react'
import { Navigate, useLocation } from 'react-router-dom'
import { useAuthStore } from './authStore'
 
interface AuthGuardProps {
  children: ReactNode
}
 
export function AuthGuard({ children }: AuthGuardProps) {
  const isAuthenticated = useAuthStore((s) => s.isAuthenticated)
  const location = useLocation()
 
  Eif (!isAuthenticated) {
    sessionStorage.setItem('returnUrl', location.pathname + location.search)
    return <Navigate to="/login" state={{ from: location }} replace />
  }
 
  return <>{children}</>
}