multica/apps/web/components/auth-guard.tsx
Naiyuan Qing ae86bba599 feat(web): add client-side auth with session storage and route protection
- Add lib/auth.ts for cookie-based session storage
- Add AuthGuard component for route protection
- Update request.ts to include sid in Authorization header
- Update login-form.tsx to save session and redirect if authenticated
- Wrap ChatPage with AuthGuard

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-13 14:31:02 +08:00

38 lines
880 B
TypeScript

'use client'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import { isAuthenticated } from '@/lib/auth'
interface AuthGuardProps {
children: React.ReactNode
}
export function AuthGuard({ children }: AuthGuardProps) {
const router = useRouter()
const [isChecking, setIsChecking] = useState(true)
const [isAuthed, setIsAuthed] = useState(false)
useEffect(() => {
if (isAuthenticated()) {
setIsAuthed(true)
} else {
router.replace('/login')
}
setIsChecking(false)
}, [router])
if (isChecking) {
return (
<div className="flex h-screen items-center justify-center bg-background">
<div className="size-6 animate-spin rounded-full border-2 border-muted-foreground border-t-transparent" />
</div>
)
}
if (!isAuthed) {
return null
}
return <>{children}</>
}