import { useEffect } from 'react'
import { useSearchParams, useNavigate } from 'react-router-dom'
import { useAuth } from '../context/AuthContext'

export default function SsoCallbackPage() {
  const [searchParams] = useSearchParams()
  const navigate = useNavigate()
  const { loginWithToken } = useAuth()

  useEffect(() => {
    const token = searchParams.get('token')
    if (token) {
      loginWithToken(token).then(() => navigate('/')).catch(() => navigate('/login?error=sso_failed'))
    } else {
      navigate('/login?error=sso_failed')
    }
  }, [])

  return (
    <div className="loading"><div className="spinner"></div> Signing in...</div>
  )
}
