Skip to content

Commit fbdf451

Browse files
authored
Implement captcha (#2882)
* web height adjustment border radius incase of dark/dim mismatch rm country codes adjust height general form refactor more form refactor refactor form submission activity indicator after finished remove remaining phone stuff adjust captcha height adjust state to reflect switch move handle to the second step pass color scheme param ts ts update state when captcha is complete web views and callbacks remove old state allow specified hosts replace phone verification with a webview * remove log * height adjustment * few changes * use the correct url * remove some debug * validate handle before continuing * explicitly check if there is a did, dont rely on error * rm throw * update allowed hosts * update redirect host for webview * fix handle * fix handle check * adjust height for full challenge
1 parent dc143d6 commit fbdf451

11 files changed

+441
-793
lines changed

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
"js-sha256": "^0.9.0",
124124
"jwt-decode": "^4.0.0",
125125
"lande": "^1.0.10",
126-
"libphonenumber-js": "^1.10.53",
127126
"lodash.chunk": "^4.2.0",
128127
"lodash.debounce": "^4.0.8",
129128
"lodash.isequal": "^4.5.0",
@@ -137,7 +136,7 @@
137136
"mobx": "^6.6.1",
138137
"mobx-react-lite": "^3.4.0",
139138
"mobx-utils": "^6.0.6",
140-
"nanoid": "^5.0.2",
139+
"nanoid": "^5.0.5",
141140
"normalize-url": "^8.0.0",
142141
"patch-package": "^6.5.1",
143142
"postinstall-postinstall": "^2.1.0",
@@ -164,6 +163,7 @@
164163
"react-native-safe-area-context": "4.8.2",
165164
"react-native-screens": "~3.29.0",
166165
"react-native-svg": "14.1.0",
166+
"react-native-ui-text-view": "link:./modules/react-native-ui-text-view",
167167
"react-native-url-polyfill": "^1.3.0",
168168
"react-native-uuid": "^2.0.1",
169169
"react-native-version-number": "^0.3.6",
@@ -178,8 +178,7 @@
178178
"tlds": "^1.234.0",
179179
"use-deep-compare": "^1.1.0",
180180
"zeego": "^1.6.2",
181-
"zod": "^3.20.2",
182-
"react-native-ui-text-view": "link:./modules/react-native-ui-text-view"
181+
"zod": "^3.20.2"
183182
},
184183
"devDependencies": {
185184
"@atproto/dev-env": "^0.2.28",

src/lib/country-codes.ts

-256
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from 'react'
2+
import {WebView, WebViewNavigation} from 'react-native-webview'
3+
import {ShouldStartLoadRequest} from 'react-native-webview/lib/WebViewTypes'
4+
import {StyleSheet} from 'react-native'
5+
import {CreateAccountState} from 'view/com/auth/create/state'
6+
7+
const ALLOWED_HOSTS = [
8+
'bsky.social',
9+
'bsky.app',
10+
'staging.bsky.app',
11+
'staging.bsky.dev',
12+
'js.hcaptcha.com',
13+
'newassets.hcaptcha.com',
14+
'api2.hcaptcha.com',
15+
]
16+
17+
export function CaptchaWebView({
18+
url,
19+
stateParam,
20+
uiState,
21+
onSuccess,
22+
onError,
23+
}: {
24+
url: string
25+
stateParam: string
26+
uiState?: CreateAccountState
27+
onSuccess: (code: string) => void
28+
onError: () => void
29+
}) {
30+
const redirectHost = React.useMemo(() => {
31+
if (!uiState?.serviceUrl) return 'bsky.app'
32+
33+
return uiState?.serviceUrl &&
34+
new URL(uiState?.serviceUrl).host === 'staging.bsky.dev'
35+
? 'staging.bsky.app'
36+
: 'bsky.app'
37+
}, [uiState?.serviceUrl])
38+
39+
const wasSuccessful = React.useRef(false)
40+
41+
const onShouldStartLoadWithRequest = React.useCallback(
42+
(event: ShouldStartLoadRequest) => {
43+
const urlp = new URL(event.url)
44+
return ALLOWED_HOSTS.includes(urlp.host)
45+
},
46+
[],
47+
)
48+
49+
const onNavigationStateChange = React.useCallback(
50+
(e: WebViewNavigation) => {
51+
if (wasSuccessful.current) return
52+
53+
const urlp = new URL(e.url)
54+
if (urlp.host !== redirectHost) return
55+
56+
const code = urlp.searchParams.get('code')
57+
if (urlp.searchParams.get('state') !== stateParam || !code) {
58+
onError()
59+
return
60+
}
61+
62+
wasSuccessful.current = true
63+
onSuccess(code)
64+
},
65+
[redirectHost, stateParam, onSuccess, onError],
66+
)
67+
68+
return (
69+
<WebView
70+
source={{uri: url}}
71+
javaScriptEnabled
72+
style={styles.webview}
73+
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
74+
onNavigationStateChange={onNavigationStateChange}
75+
scrollEnabled={false}
76+
/>
77+
)
78+
}
79+
80+
const styles = StyleSheet.create({
81+
webview: {
82+
flex: 1,
83+
backgroundColor: 'transparent',
84+
borderRadius: 10,
85+
},
86+
})

0 commit comments

Comments
 (0)