cleanup captcha step
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import React, {useEffect, useRef} from 'react'
|
import {useEffect, useMemo, useRef} from 'react'
|
||||||
import {StyleSheet} from 'react-native'
|
|
||||||
import {WebView, type WebViewNavigation} from 'react-native-webview'
|
import {WebView, type WebViewNavigation} from 'react-native-webview'
|
||||||
import {type ShouldStartLoadRequest} from 'react-native-webview/lib/WebViewTypes'
|
import {type ShouldStartLoadRequest} from 'react-native-webview/lib/WebViewTypes'
|
||||||
|
|
||||||
@@ -43,7 +42,7 @@ export function CaptchaWebView({
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const redirectHost = React.useMemo(() => {
|
const redirectHost = useMemo(() => {
|
||||||
if (!state?.serviceUrl) return 'bsky.app'
|
if (!state?.serviceUrl) return 'bsky.app'
|
||||||
|
|
||||||
return state?.serviceUrl &&
|
return state?.serviceUrl &&
|
||||||
@@ -52,49 +51,47 @@ export function CaptchaWebView({
|
|||||||
: 'bsky.app'
|
: 'bsky.app'
|
||||||
}, [state?.serviceUrl])
|
}, [state?.serviceUrl])
|
||||||
|
|
||||||
const wasSuccessful = React.useRef(false)
|
const wasSuccessful = useRef(false)
|
||||||
|
|
||||||
const onShouldStartLoadWithRequest = React.useCallback(
|
const onShouldStartLoadWithRequest = (event: ShouldStartLoadRequest) => {
|
||||||
(event: ShouldStartLoadRequest) => {
|
const urlp = new URL(event.url)
|
||||||
const urlp = new URL(event.url)
|
return ALLOWED_HOSTS.includes(urlp.host)
|
||||||
return ALLOWED_HOSTS.includes(urlp.host)
|
}
|
||||||
},
|
|
||||||
[],
|
|
||||||
)
|
|
||||||
|
|
||||||
const onNavigationStateChange = React.useCallback(
|
const onNavigationStateChange = (e: WebViewNavigation) => {
|
||||||
(e: WebViewNavigation) => {
|
if (wasSuccessful.current) return
|
||||||
if (wasSuccessful.current) return
|
|
||||||
|
|
||||||
const urlp = new URL(e.url)
|
const urlp = new URL(e.url)
|
||||||
if (urlp.host !== redirectHost || urlp.pathname === '/gate/signup') return
|
if (urlp.host !== redirectHost || urlp.pathname === '/gate/signup') return
|
||||||
|
|
||||||
const code = urlp.searchParams.get('code')
|
const code = urlp.searchParams.get('code')
|
||||||
if (urlp.searchParams.get('state') !== stateParam || !code) {
|
if (urlp.searchParams.get('state') !== stateParam || !code) {
|
||||||
onError({error: 'Invalid state or code'})
|
onError({error: 'Invalid state or code'})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// We want to delay the completion of this screen ever so slightly so that it doesn't appear to be a glitch if it completes too fast
|
// We want to delay the completion of this screen ever so slightly so that it doesn't appear to be a glitch if it completes too fast
|
||||||
wasSuccessful.current = true
|
wasSuccessful.current = true
|
||||||
const now = Date.now()
|
const now = Date.now()
|
||||||
const timeTaken = now - startedAt.current
|
const timeTaken = now - startedAt.current
|
||||||
if (timeTaken < MIN_DELAY) {
|
if (timeTaken < MIN_DELAY) {
|
||||||
successTo.current = setTimeout(() => {
|
successTo.current = setTimeout(() => {
|
||||||
onSuccess(code)
|
|
||||||
}, MIN_DELAY - timeTaken)
|
|
||||||
} else {
|
|
||||||
onSuccess(code)
|
onSuccess(code)
|
||||||
}
|
}, MIN_DELAY - timeTaken)
|
||||||
},
|
} else {
|
||||||
[redirectHost, stateParam, onSuccess, onError],
|
onSuccess(code)
|
||||||
)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<WebView
|
<WebView
|
||||||
source={{uri: url}}
|
source={{uri: url}}
|
||||||
javaScriptEnabled
|
javaScriptEnabled
|
||||||
style={styles.webview}
|
style={{
|
||||||
|
flex: 1,
|
||||||
|
backgroundColor: 'transparent',
|
||||||
|
borderRadius: 10,
|
||||||
|
}}
|
||||||
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
|
||||||
onNavigationStateChange={onNavigationStateChange}
|
onNavigationStateChange={onNavigationStateChange}
|
||||||
scrollEnabled={false}
|
scrollEnabled={false}
|
||||||
@@ -107,11 +104,3 @@ export function CaptchaWebView({
|
|||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const styles = StyleSheet.create({
|
|
||||||
webview: {
|
|
||||||
flex: 1,
|
|
||||||
backgroundColor: 'transparent',
|
|
||||||
borderRadius: 10,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|||||||
Reference in New Issue
Block a user