-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
233 lines (205 loc) · 5.34 KB
/
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import React, {Component} from 'react';
import {Text, View, TextInput, TouchableOpacity, WebView, Modal, StyleSheet} from 'react-native';
import url from 'url';
import firebase from 'firebase';
const captchaUrl = ``// link to your captcha.html
export default class App extends Component {
constructor (props) {
super(props);
this.state = {
showModal: false,
codeIsSent: false,
confirmation: {},
errorMessage: ""
}
}
componentDidMount () {
firebase.initializeApp({
apiKey: "****************************",
authDomain: "*******************",
databaseURL: "****************",
projectId: "***********",
storageBucket: "************",
messagingSenderId: "********"
})
}
_handleResponse = data => {
let query = url.parse(data.url, true).query;
if (query.hasOwnProperty('token')) {
this._sendConfirmationCode(query.token);
} else if (query.hasOwnProperty('cancel')) {
this.setState({ showModal: false});
}
}
_signUp = () => {
if (this.state.codeIsSent) {
this._confirmCode();
}else {
this.setState({
showModal: true
})
}
}
_sendConfirmationCode = (captchaToken) => {
this.setState({ showModal: false });
let number = `+${this.state.input_value}`;
const captchaVerifier = {
type: 'recaptcha',
verify: () => Promise.resolve(captchaToken)
}
firebase.auth().signInWithPhoneNumber(number, captchaVerifier)
.then((confirmation) => {
this.setState({confirmation, codeIsSent: true, input_value: "", errorMessage: ""})
})
.catch((err) => {
this.setState({errorMessage: "Oops! something is wrong"});
});
}
_confirmCode = () => {
this.state.confirmation.confirm(this.state.input_value)
.then((result) => {
this.setState({isAuthenticated: true});
})
.catch((err) => {
this.setState({errorMessage: "Oops! something is wrong"});
});
}
renderCaptchScreen = () => {
return (
<View style={{ marginTop: 100 }}>
<Modal
visible={this.state.showModal}
onRequestClose={() => this.setState({ showModal: false })}
>
<WebView
source={{ uri: captchaUrl }}
onNavigationStateChange={data =>
this._handleResponse(data)
}
injectedJavaScript={`document.f1.submit()`}
/>
</Modal>
</View>
)
}
renderLoginScreen = () => {
const {
container,
headerText,
descriptionText,
phoneNumberContainer,
phoneNumberText,
errorMessage,
errorMessageText,
continueButtonContainer,
continueButton
} = styles;
return (
<View style={container}>
<Text style={headerText}>Welcome to Firebase phone auth</Text>
{
this.state.codeIsSent ?
<Text style={descriptionText}>Please Enter your confirmation code</Text>
:
<Text style={descriptionText}>Please Enter your phone number to continue</Text>
}
<View stlye={phoneNumberContainer}>
<TextInput
style={phoneNumberText}
placeholder={this.state.codeIsSent ? 'Confirmation Code' : 'Phone Number'}
placeholderTextColor={'#a9a9a9'}
keyboardType={'numeric'}
value={this.state.input_value}
onChangeText={input_value => this.setState({input_value})}
>
</TextInput>
</View>
{
this.state.errorMessage ?
<View>
<Text style={errorMessageText}>{this.state.errorMessage}</Text>
</View>
: null
}
<View
style={continueButtonContainer}
>
<TouchableOpacity
onPress={this._signUp}
>
<Text style={continueButton}>Continue</Text>
</TouchableOpacity>
</View>
{this.renderCaptchScreen()}
</View>
)
}
renderMainScreen = () => {
const {
container,
headerText
} = styles;
return (
<View style={container}>
<Text style={headerText}>You are successfully signed In</Text>
</View>
)
}
render() {
return (
this.state.isAuthenticated ? this.renderMainScreen() : this.renderLoginScreen()
);
}
}
const styles = StyleSheet.create({
container: {
height: "100%",
padding: 10,
backgroundColor: 'white',
alignItems: 'center',
},
headerText: {
color: '#135cb3',
fontSize: 25,
textAlign: "center",
fontWeight: '500',
marginBottom: 5
},
descriptionText: {
fontSize: 16,
marginBottom: 20
},
phoneNumberContainer: {
flexDirection: 'row',
paddingHorizontal: 16,
marginBottom: 20,
},
phoneNumberText: {
borderColor: '#8c8c8c',
borderBottomWidth: 1,
fontSize: 16,
minWidth: 180
},
continueButtonContainer: {
marginTop: 20,
paddingHorizontal: 22,
paddingVertical: 10,
width: 100,
alignItems: "center",
},
continueButton:{
backgroundColor: '#2b7cd9',
borderRadius: 24,
fontSize: 16,
paddingTop: 12,
minWidth: 100,
color: "#fff",
textAlign: "center",
justifyContent: "center"
},
errorMessageText: {
marginTop: 10,
color: "red",
fontSize: 18
}
})