Skip to content

Commit

Permalink
feat: Add session code inAppBrowser url
Browse files Browse the repository at this point in the history
This allows bi webviews or oauth pages to be displayed in inAppBrowser
without user login twice
  • Loading branch information
doubleface authored and doubleface committed Jun 13, 2022
1 parent 6b78964 commit e2037ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
19 changes: 15 additions & 4 deletions packages/cozy-harvest-lib/src/components/InAppBrowser.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions packages/cozy-harvest-lib/src/components/InAppBrowser.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<WebviewIntentProvider webviewService={webviewService}>
<InAppBrowser url={url} />
</WebviewIntentProvider>
)

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 () => {
Expand Down

0 comments on commit e2037ef

Please sign in to comment.