-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathExample.UserJourneys.App.js
More file actions
113 lines (102 loc) · 2.68 KB
/
Example.UserJourneys.App.js
File metadata and controls
113 lines (102 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { useRef, useState } from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import ConfirmHcaptcha, { initJourneyTracking } from '@hcaptcha/react-native-hcaptcha';
// demo sitekey
const siteKey = '00000000-0000-0000-0000-000000000000';
const baseUrl = 'https://hcaptcha.com';
initJourneyTracking({
debug: true,
onStats: (stats) => {
console.log('[journey-stats]', JSON.stringify(stats));
},
});
const App = () => {
const [code, setCode] = useState(null);
const [warmups, setWarmups] = useState(0);
const captchaForm = useRef(null);
const onMessage = (event) => {
if (!event?.nativeEvent?.data) {
return;
}
if (event.nativeEvent.data === 'open') {
console.log('Visual challenge opened');
return;
}
if (event.success) {
setCode(event.nativeEvent.data);
captchaForm.current.hide();
event.markUsed();
console.log('Verified code from hCaptcha', event.nativeEvent.data);
return;
}
if (event.nativeEvent.data === 'challenge-expired') {
event.reset();
console.log('Visual challenge expired, reset...', event.nativeEvent.data);
return;
}
setCode(event.nativeEvent.data);
captchaForm.current.hide();
console.log('Verification failed', event.nativeEvent.data);
};
return (
<View style={styles.container}>
<ConfirmHcaptcha
ref={captchaForm}
baseUrl={baseUrl}
languageCode="en"
onMessage={onMessage}
siteKey={siteKey}
userJourney={true}
/>
<TouchableOpacity
nativeID="warmup-touch"
onPress={() => {
setWarmups((value) => value + 1);
}}
testID="warmup-touch"
>
<Text style={styles.paragraph}>Preverify: {warmups} taps here</Text>
</TouchableOpacity>
<TouchableOpacity
nativeID="launch-captcha"
onPress={() => {
captchaForm.current.show();
}}
testID="launch-captcha"
>
<Text style={styles.paragraph}>Tap to launch</Text>
</TouchableOpacity>
{code && (
<Text style={styles.codeContainer}>
{'passcode or status: '}
<Text style={styles.codeText}>
{code}
</Text>
</Text>
)}
</View>
);
};
const styles = StyleSheet.create({
codeContainer: {
alignSelf: 'center',
},
codeText: {
color: 'darkviolet',
fontSize: 6,
fontWeight: 'bold',
},
container: {
backgroundColor: '#ecf0f1',
flex: 1,
justifyContent: 'center',
padding: 8,
},
paragraph: {
fontSize: 18,
fontWeight: 'bold',
margin: 24,
textAlign: 'center',
},
});
export default App;