From e2037efabd73d93106d2f88bb122f75e783afb6e Mon Sep 17 00:00:00 2001 From: doubleface Date: Wed, 8 Jun 2022 18:07:28 +0200 Subject: [PATCH] feat: Add session code inAppBrowser url This allows bi webviews or oauth pages to be displayed in inAppBrowser without user login twice --- .../src/components/InAppBrowser.jsx | 19 +++++++++++++++---- .../src/components/InAppBrowser.spec.jsx | 13 +++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/packages/cozy-harvest-lib/src/components/InAppBrowser.jsx b/packages/cozy-harvest-lib/src/components/InAppBrowser.jsx index e91a116b72..367837383f 100644 --- a/packages/cozy-harvest-lib/src/components/InAppBrowser.jsx +++ b/packages/cozy-harvest-lib/src/components/InAppBrowser.jsx @@ -9,11 +9,22 @@ const InAppBrowser = ({ url, onClose }) => { useEffect(() => { async function insideEffect() { if (webviewIntent) { - const result = await webviewIntent.call('showInAppBrowser', { url }) - if (result?.type === 'cancel' && onClose) { + try { + const sessionCode = await webviewIntent.call('fetchSessionCode') + logger.debug('got session code', sessionCode) + const iabUrl = new URL(url) + iabUrl.searchParams.append('session_code', sessionCode) + const result = await webviewIntent.call('showInAppBrowser', { + url: iabUrl.toString() + }) + if (result?.type !== 'dismiss' && result?.type !== 'cancel') { + logger.error('Unexpected InAppBrowser result', result) + } + } catch (err) { + logger.error('unexpected fetchSessionCode result', err) + } + if (onClose) { onClose() - } else if (result?.type !== 'dismiss') { - logger.error('Unexpected InAppBrowser result', result) } } } diff --git a/packages/cozy-harvest-lib/src/components/InAppBrowser.spec.jsx b/packages/cozy-harvest-lib/src/components/InAppBrowser.spec.jsx index e501f478d8..9f931d876d 100644 --- a/packages/cozy-harvest-lib/src/components/InAppBrowser.spec.jsx +++ b/packages/cozy-harvest-lib/src/components/InAppBrowser.spec.jsx @@ -4,21 +4,26 @@ import InAppBrowser from './InAppBrowser' import { WebviewIntentProvider } from 'cozy-intent' describe('InAppBrowser', () => { - it('should call showInAppBrowser', async () => { + it('should call fetchSessionCode and showInAppBrowser', async () => { const url = 'https://test.url' const intentCall = jest.fn() const webviewService = { call: intentCall } - intentCall.mockResolvedValue({ type: 'dismiss' }) + intentCall + .mockResolvedValueOnce('sessioncode') + .mockResolvedValueOnce({ type: 'dismiss' }) render( ) - expect(webviewService.call).toHaveBeenNthCalledWith(1, 'showInAppBrowser', { - url + await waitFor(() => expect(webviewService.call).toHaveBeenCalled()) + + expect(webviewService.call).toHaveBeenNthCalledWith(1, 'fetchSessionCode') + expect(webviewService.call).toHaveBeenNthCalledWith(2, 'showInAppBrowser', { + url: url + '/?session_code=sessioncode' }) }) it('should call onClose when user closes the inAppBrowser in app', async () => {