Skip to content

Commit

Permalink
update torus, handle no supported browser error, show dialog with sug…
Browse files Browse the repository at this point in the history
…gestions (#3276)
  • Loading branch information
serdiukov-o-nordwhale committed Jun 20, 2021
1 parent a416948 commit f63d499
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"@sentry/browser": "^5.10.2",
"@sentry/react-native": "^2.1.0",
"@svgr/webpack": "^5.3.0",
"@toruslabs/torus-direct-react-native-sdk": "^0.4.1",
"@toruslabs/torus-direct-react-native-sdk": "^0.5.0",
"@toruslabs/torus-direct-web-sdk": "^4.14.0",
"@tradle/react-native-http": "^2.0.1",
"Base64": "^1.1.0",
Expand Down
52 changes: 39 additions & 13 deletions src/components/auth/torus/AuthTorus.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React, { useCallback, useEffect, useState } from 'react'
import { Paragraph } from 'react-native-paper'
import { View } from 'react-native'
import { Platform, View } from 'react-native'
import { get } from 'lodash'
import AsyncStorage from '../../../lib/utils/asyncStorage'
import logger from '../../../lib/logger/pino-logger'
Expand Down Expand Up @@ -40,8 +40,6 @@ import SignUpIn from '../login/SignUpScreen'

import LoadingIcon from '../../common/modal/LoadingIcon'

// import SpinnerCheckMark from '../../common/animations/SpinnerCheckMark'

import { timeout } from '../../../lib/utils/async'
import DeepLinking from '../../../lib/utils/deepLinking'

Expand Down Expand Up @@ -144,8 +142,9 @@ const AuthTorus = ({ screenProps, navigation, styles, store }) => {
async provider => {
if (['development', 'test'].includes(config.env)) {
const torusUser = await AsyncStorage.getItem('TorusTestUser')

if (torusUser != null) {
return Promise.resolve(torusUser)
return torusUser
}
}

Expand Down Expand Up @@ -199,6 +198,25 @@ const AuthTorus = ({ screenProps, navigation, styles, store }) => {
})
}

const showSignupError = useCallback(
(exception = null) => {
let suggestion = 'Please try again.'
const { message = '' } = exception || {}

if (message.includes('NoAllowedBrowserFoundException')) {
const suggestedBrowser = Platform.select({
ios: 'Safari',
android: 'Chrome',
})

suggestion = `Your default browser isn't supported. Please, set ${suggestedBrowser} as default and try again.`
}

showErrorDialog(`We were unable to complete the signup. ${suggestion}`)
},
[showErrorDialog],
)

const showNotSignedUp = provider => {
let resolve
const promise = new Promise((res, rej) => {
Expand Down Expand Up @@ -288,7 +306,7 @@ const AuthTorus = ({ screenProps, navigation, styles, store }) => {
AsyncStorage.setItem('recallTorusRedirectProvider', provider)
authScreen && AsyncStorage.setItem('recallTorusRedirectScreen', authScreen)

await getTorusUser(provider)
await getTorusUser(provider).catch(showSignupError)
return
}

Expand All @@ -297,30 +315,38 @@ const AuthTorus = ({ screenProps, navigation, styles, store }) => {
provider = await AsyncStorage.getItem('recallTorusRedirectProvider')
}

const { torusUser, replacing } = await handleTorusResponse(
torusUserRedirectPromise || getTorusUser(provider),
provider,
)
let torusResponse

try {
torusResponse = await handleTorusResponse(torusUserRedirectPromise || getTorusUser(provider), provider)

if (torusUser == null) {
showErrorDialog('We were unable to complete the signup. Please try again.')
if (get(torusResponse, 'torusUser') == null) {
throw new Error('Invalid Torus response.')
}
} catch (exception) {
showSignupError(exception)
return
}

const { torusUser, replacing } = torusResponse
const existsResult = await userExists(torusUser)
log.debug('checking userAlreadyExist', { isSignup, existsResult })
let selection = authScreen

log.debug('checking userAlreadyExist', { isSignup, existsResult })

if (isSignup) {
//if user identifier exists or email/mobile found in another account
// if user identifier exists or email/mobile found in another account
if (existsResult.exists) {
selection = await showAlreadySignedUp(provider, existsResult)

if (selection === 'signin') {
return setAuthScreen('signin')
}
}
} else if (isSignup === false && existsResult.identifier !== true) {
//no account with identifier found = user didn't signup
selection = await showNotSignedUp(provider)

return setAuthScreen(selection)
}

Expand Down
6 changes: 5 additions & 1 deletion src/components/auth/torus/sdk/torus.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ class Torus {
return loginOptions
}

return { ...loginOptions, preferCustomTabs: true }
return {
...loginOptions,
preferCustomTabs: true,
allowedBrowsers: ['com.android.chrome', 'com.google.android.apps.chrome', 'com.android.chrome.beta'],
}
}
}

Expand Down

0 comments on commit f63d499

Please sign in to comment.