-
Notifications
You must be signed in to change notification settings - Fork 21
/
Example.App.js
80 lines (75 loc) · 2.28 KB
/
Example.App.js
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
import * as React from 'react';
import { Text, View, StyleSheet, TouchableOpacity } from 'react-native';
import ConfirmHcaptcha from '@hcaptcha/react-native-hcaptcha';
// demo sitekey
const siteKey = '00000000-0000-0000-0000-000000000000';
const baseUrl = 'https://hcaptcha.com';
export default class App extends React.Component {
state = {
code: null,
};
onMessage = event => {
if (event && event.nativeEvent.data) {
if (['cancel'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
this.setState({ code: event.nativeEvent.data });
} else if (['error'].includes(event.nativeEvent.data)) {
this.captchaForm.hide();
this.setState({ code: event.nativeEvent.data });
console.log('Verification failed', event.nativeEvent.data);
} else if (event.nativeEvent.data === 'expired') {
event.reset();
console.log('Visual challenge expired, reset...', event.nativeEvent.data);
} else if (event.nativeEvent.data === 'open') {
console.log('Visual challenge opened');
} else {
console.log('Verified code from hCaptcha', event.nativeEvent.data);
this.captchaForm.hide();
event.markUsed();
this.setState({ code: event.nativeEvent.data });
}
}
};
render() {
let { code } = this.state;
return (
<View style={styles.container}>
<ConfirmHcaptcha
ref={_ref => (this.captchaForm = _ref)}
siteKey={siteKey}
baseUrl={baseUrl}
languageCode="en"
onMessage={this.onMessage}
/>
<TouchableOpacity
onPress={() => {
this.captchaForm.show();
}}>
<Text style={styles.paragraph}>Click to launch</Text>
</TouchableOpacity>
{code && (
<Text style={{ alignSelf: 'center' }}>
{`passcode or status: `}
<Text style={{ color: 'darkviolet', fontWeight: 'bold', fontSize: 6 }}>
{code}
</Text>
</Text>
)}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
padding: 8,
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
},
});