Skip to content

Commit accd846

Browse files
committed
feat(access): if a project user can't be found envoke dashboard request-access flow
1 parent 1668657 commit accd846

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/react/src/components/auth/LoginError.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ vi.mock('../../hooks/auth/useLogOut', () => ({
99
useLogOut: vi.fn(() => async () => {}),
1010
}))
1111

12+
vi.mock('../../hooks/comlink/useWindowConnection', () => ({
13+
useWindowConnection: vi.fn(() => ({fetch: vi.fn()})),
14+
}))
15+
1216
describe('LoginError', () => {
1317
it('shows authentication error and retry button', async () => {
1418
const mockReset = vi.fn()
@@ -21,6 +25,7 @@ describe('LoginError', () => {
2125
)
2226

2327
expect(screen.getByText('Authentication Error')).toBeInTheDocument()
28+
2429
const retryButton = screen.getByRole('button', {name: 'Retry'})
2530
fireEvent.click(retryButton)
2631

packages/react/src/components/auth/LoginError.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {ClientError} from '@sanity/client'
2+
import {SDK_CHANNEL_NAME, SDK_NODE_NAME} from '@sanity/message-protocol'
23
import {
34
AuthStateType,
45
getClientErrorApiBody,
@@ -10,6 +11,8 @@ import {type FallbackProps} from 'react-error-boundary'
1011

1112
import {useAuthState} from '../../hooks/auth/useAuthState'
1213
import {useLogOut} from '../../hooks/auth/useLogOut'
14+
import {useWindowConnection} from '../../hooks/comlink/useWindowConnection'
15+
import {useSanityInstance} from '../../hooks/context/useSanityInstance'
1316
import {Error} from '../errors/Error'
1417
import {AuthError} from './AuthError'
1518
import {ConfigurationError} from './ConfigurationError'
@@ -36,12 +39,23 @@ export function LoginError({error, resetErrorBoundary}: LoginErrorProps): React.
3639

3740
const logout = useLogOut()
3841
const authState = useAuthState()
42+
const {
43+
config: {projectId},
44+
} = useSanityInstance()
3945

4046
const [authErrorMessage, setAuthErrorMessage] = useState(
4147
'Please try again or contact support if the problem persists.',
4248
)
4349
const [showRetryCta, setShowRetryCta] = useState(true)
4450

51+
/**
52+
* TODO: before merge update message-protocol package to include the new message type
53+
*/
54+
const {fetch} = useWindowConnection({
55+
name: SDK_NODE_NAME,
56+
connectTo: SDK_CHANNEL_NAME,
57+
})
58+
4559
const handleRetry = useCallback(async () => {
4660
await logout()
4761
resetErrorBoundary()
@@ -55,6 +69,13 @@ export function LoginError({error, resetErrorBoundary}: LoginErrorProps): React.
5569
const description = getClientErrorApiDescription(error)
5670
if (description) setAuthErrorMessage(description)
5771
setShowRetryCta(false)
72+
/**
73+
* Handoff to dashboard to enable the request access flow for the project.
74+
*/
75+
fetch('dashboard/v1/auth/access/request', {
76+
resourceType: 'project',
77+
resourceId: projectId,
78+
})
5879
} else {
5980
setShowRetryCta(true)
6081
handleRetry()
@@ -73,7 +94,7 @@ export function LoginError({error, resetErrorBoundary}: LoginErrorProps): React.
7394
setAuthErrorMessage(error.message)
7495
setShowRetryCta(true)
7596
}
76-
}, [authState, handleRetry, error])
97+
}, [authState, handleRetry, error, fetch, projectId])
7798

7899
return (
79100
<Error

0 commit comments

Comments
 (0)