All files / src/features/auth/routes GoogleCompleteRoute.tsx

100% Statements 13/13
100% Branches 14/14
100% Functions 3/3
100% Lines 12/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 29 30 31 32 33 34                  6x 6x 6x 19x 6x     6x   6x 5x 1x 1x     6x 6x                
import { useEffect } from 'react'
import { useQuery } from '@tanstack/react-query'
import { useTranslation } from 'react-i18next'
import { Link, useNavigate, useSearchParams } from 'react-router-dom'
import { useAuthIdentityStore } from '@/shared/stores/authIdentity.store'
import { getCurrentUser } from '../api/auth.api'
import { googleAuthQueryKeys } from '../api/google-auth.hooks'
 
export function GoogleCompleteRoute() {
  const { t } = useTranslation()
  const [params] = useSearchParams()
  const navigate = useNavigate()
  const setIdentity = useAuthIdentityStore((state) => state.setIdentity)
  const error = params.get('error')
  // A fresh request confirms the session; neither URL parameters nor cached
  // identity from another account may authorize navigation into the app.
  const user = useQuery({ queryKey: googleAuthQueryKeys.complete, queryFn: getCurrentUser,
    enabled: !error, retry: false, staleTime: 0, gcTime: 0, meta: { expectsAnonymous: true } })
  useEffect(() => {
    if (!error && user.data) {
      setIdentity(user.data)
      void navigate('/dashboard', { replace: true })
    }
  }, [error, user.data, navigate, setIdentity])
  const failed = Boolean(error) || user.isError
  return <div className="space-y-4 py-7">
    <h1 className="font-display text-2xl font-bold text-ink">{t('auth.social.google')}</h1>
    <p role={failed ? 'alert' : 'status'} className="text-sm text-ink-muted">
      {t(failed ? error === 'email_verification_required' ? 'auth.social.googleVerifyEmail' : 'auth.social.googleError' : 'auth.social.googleCompleting')}
    </p>
    {failed && <Link to="/login" replace className="inline-block rounded font-semibold text-primary underline focus-visible:outline-2">{t('auth.social.googleBack')}</Link>}
  </div>
}