All files / features / auth AuthGuard.tsx

80% Statements 24/30
56.25% Branches 9/16
85.71% Functions 6/7
85.18% Lines 23/27

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      56x   56x   302x 819x 302x 302x 4x 2x   4x              
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()
 
  if (!isAuthenticated) {
    if (!sessionStorage.getItem('returnUrl')) {
      sessionStorage.setItem('returnUrl', location.pathname + location.search)
    }
    return <Navigate to="/login" replace />
  }
 
  return <>{children}</>
}