11import { ClientError } from '@sanity/client'
2- import { AuthStateType } from '@sanity/sdk'
2+ import {
3+ AuthStateType ,
4+ getClientErrorApiBody ,
5+ getClientErrorApiDescription ,
6+ isProjectUserNotFoundClientError ,
7+ } from '@sanity/sdk'
38import { useCallback , useEffect , useState } from 'react'
49import { type FallbackProps } from 'react-error-boundary'
510
@@ -35,6 +40,7 @@ export function LoginError({error, resetErrorBoundary}: LoginErrorProps): React.
3540 const [ authErrorMessage , setAuthErrorMessage ] = useState (
3641 'Please try again or contact support if the problem persists.' ,
3742 )
43+ const [ showRetryCta , setShowRetryCta ] = useState ( true )
3844
3945 const handleRetry = useCallback ( async ( ) => {
4046 await logout ( )
@@ -44,29 +50,43 @@ export function LoginError({error, resetErrorBoundary}: LoginErrorProps): React.
4450 useEffect ( ( ) => {
4551 if ( error instanceof ClientError ) {
4652 if ( error . statusCode === 401 ) {
47- handleRetry ( )
53+ // Surface a friendly message for projectUserNotFoundError (do not logout/refresh)
54+ if ( isProjectUserNotFoundClientError ( error ) ) {
55+ const description = getClientErrorApiDescription ( error )
56+ if ( description ) setAuthErrorMessage ( description )
57+ setShowRetryCta ( false )
58+ } else {
59+ setShowRetryCta ( true )
60+ handleRetry ( )
61+ }
4862 } else if ( error . statusCode === 404 ) {
49- const errorMessage = error . response . body . message || ''
63+ const errorMessage = getClientErrorApiBody ( error ) ? .message || ''
5064 if ( errorMessage . startsWith ( 'Session with sid' ) && errorMessage . endsWith ( 'not found' ) ) {
5165 setAuthErrorMessage ( 'The session ID is invalid or expired.' )
5266 } else {
5367 setAuthErrorMessage ( 'The login link is invalid or expired. Please try again.' )
5468 }
69+ setShowRetryCta ( true )
5570 }
5671 }
5772 if ( authState . type !== AuthStateType . ERROR && error instanceof ConfigurationError ) {
5873 setAuthErrorMessage ( error . message )
74+ setShowRetryCta ( true )
5975 }
6076 } , [ authState , handleRetry , error ] )
6177
6278 return (
6379 < Error
6480 heading = { error instanceof AuthError ? 'Authentication Error' : 'Configuration Error' }
6581 description = { authErrorMessage }
66- cta = { {
67- text : 'Retry' ,
68- onClick : handleRetry ,
69- } }
82+ cta = {
83+ showRetryCta
84+ ? {
85+ text : 'Retry' ,
86+ onClick : handleRetry ,
87+ }
88+ : undefined
89+ }
7090 />
7191 )
7292}
0 commit comments