-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
441 lines (380 loc) · 11.5 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, AsyncStorage, Alert, TextInput, Button} from 'react-native';
import firebase from 'react-native-firebase';
// import Navigation from './navigation/Navigation';
import DeviceInfo from 'react-native-device-info';
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation';
import ListScreen from './ListScreen';
import AWSSignature from 'react-native-aws-signature';
import Loader from './Loader';
import WebviewScreen from './WebviewScreen';
import axios from 'axios';
Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
class HomeScreen extends Component<Props> {
constructor(props) {
super(props);
this.state = {
username: '',
password: '',
loginStatus: false,
loader: false
};
this.validate = this.validate.bind(this);
this.validateGuid = this.validateGuid.bind(this);
// AsyncStorage.clear(async function(){
//});
//Alert.alert(JSON.stringify(userToken));
// This will switch to the App screen or Auth screen and this loading
// screen will be unmounted and thrown away.
}
static navigationOptions = {
header: null
}
validateGuid = async () => {
var thisobj = this;
var loginToken = await AsyncStorage.getItem("loginToken");
await Alert.alert("login token "+JSON.stringify(loginToken));
//console.log('login token', loginToken);
if(loginToken != ""){
DeviceInfo.getMACAddress().then(mac => {
//Alert.alert("mac "+mac);
axios.get('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/AuthMobileApp.aspx?AppID=Mobile&MacAddress='+mac+'&AuthKey=' +loginToken )
.then(async function(responseData){
await Alert.alert("login token "+responseData.data);
//console.log('verify res', responseData);
if(responseData.data){
thisobj.props.navigation.navigate('List');
}else {
AsyncStorage.removeItem("loginToken");
thisobj.props.navigation.navigate('App');
}
});
// "E5:12:D8:E5:69:97"
});
}else{
this.props.navigation.navigate('App');
}
}
_bootstrapAsync = async () => {
this.setState({
loader:true
})
const dname = DeviceInfo.getDeviceName();
//const mac = DeviceInfo.getMACAddress();
const appName = DeviceInfo.getApplicationName();
DeviceInfo.getMACAddress().then(mac => {
// "E5:12:D8:E5:69:97"
console.log('mac', mac);
var payload = {
AppID: 'android',
MacAddress: mac,
DeviceName: appName,
ID: this.state.username,
PASS: this.state.password,
};
var thisobj = this;
// fetch('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/Login.aspx?AppID=Mobile&MacAddress='+mac+'&DeviceName='+appName+'&ID='+this.state.username+'&PASS='+this.state.password,{
// headers: {
// 'Accept':'application/json'
// },
// })
axios.get('https://mdev.humaxdigital.com/HumaxMobile/NonAuth/Login.aspx?AppID=Mobile&MacAddress='+mac+'&DeviceName='+appName+'&ID='+this.state.username+'&PASS='+this.state.password )
.then(async function(responseData){
var token = responseData.data;
let fcmToken = await AsyncStorage.getItem('fcmToken');
console.log("guid", token);
const path = '/production';
const url = 'https://5q6xg017b6.execute-api.us-east-1.amazonaws.com'+ path;
const credentials = {
AccessKeyId: '48pbeVmxzJ8Ubgr3ocQhg85gNlqIF4Z6iRNP1A8k',
SecretKey: 'AKIAJ4FEQP7TFAA6A6CA'
}
// code to save token to AWS
let auth_date = new Date();
let auth_header = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'dataType': 'json',
'X-Amz-Date': auth_date.toISOString(),
'host': '5q6xg017b6.execute-api.us-east-1.amazonaws.com'
}
let auth_options = {
path: path,
method: 'POST',
service: 'execute-api',
headers: auth_header,
region: 'us-east-1',
body: '',
credentials
};
let awsSignature = new AWSSignature();
awsSignature.setParams(auth_options);
const authorization = awsSignature.getAuthorizationHeader();
// Add the authorization to the header
auth_header['Authorization'] = authorization['Authorization']
let options = Object.assign({
method: 'POST',
headers: auth_header,
body: JSON.stringify({
"username": thisobj.state.username,
"tokenID": fcmToken,
"guid": token,
"deviceType": "android",
"deviceModel": appName,
"macAddress": mac
})
});
//const url = 'https://' + '5q6xg017b6.execute-api.us-east-1.amazonaws.com/production';
console.log(options);
fetch(url, options).then(async function(resp){
console.log("second hit");
const responseData = await resp.json();
await thisobj.setState({loginStatus: true});
//let resObj = JSON.stringify(responseData);
//console.log("AWS "+ JSON.stringify(responseData));
console.log(responseData.body);
if(responseData.body == 'Success'){
console.log('for setting instorage', token);
await AsyncStorage.setItem('loginToken', token);
var getting = await AsyncStorage.getItem('loginToken');
console.log('from loacl storage', getting);
thisobj.props.navigation.navigate('List');
}
})
//code end
}
)
});
};
//On click of login btn
onLogin() {
if(this.validate()){
const { username, password } = this.state;
this._bootstrapAsync();
}
else{
Alert.alert('Both fields are required');
}
}
//Validate input field
validate() {
const { username, password } = this.state;
if(username === '' || password === ''){
return false;
}
else{
return true;
}
}
async componentDidMount(){
this.checkLogin();
this.checkPermission();
this.createNotificationListeners();
}
////////////////////// Add these methods //////////////////////
//Remove listeners allocated in createNotificationListeners()
componentWillUnmount() {
this.notificationListener();
this.notificationOpenedListener();
}
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
this.showAlert(title, body);
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
// const notificationOpen = await firebase.notifications().getInitialNotification();
// if (notificationOpen) {
// const { title, body } = notificationOpen.notification;
// this.showAlert(title, body);
// }
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
console.log(JSON.stringify(message));
});
}
showAlert(title, body) {
Alert.alert(
title, body,
[
{ text: 'OK', onPress: () => console.log('OK Pressed') },
],
{ cancelable: false },
);
}
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
//3
async getToken() {
let fcmToken = await AsyncStorage.getItem('fcmToken');
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
// Alert.alert("Device Token",fcmToken);
console.log('token', fcmToken);
if (fcmToken) {
// user has a device token
await AsyncStorage.setItem('fcmToken', fcmToken);
}
}
else{
// Alert.alert("Device Token",fcmToken);
console.log('token', fcmToken);
}
}
//2
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
_list = async () => {
await AsyncStorage.clear();
this.props.navigation.navigate('Auth');
};
async checkLogin() {
let loginToken = await AsyncStorage.getItem('loginToken');
if(loginToken){
this.validateGuid();
this.setState({loginStatus : true});
}
else{
this.setState({loginStatus : false});
}
}
render() {
const {loader} = this.state;
return (
<View style={styles.main}>
<Text style={styles.heading}>
Welcome to Humax Security</Text>
<View style={{alignItems: 'center', justifyContent: 'center' }}>
<TextInput
value={this.state.username}
onChangeText={(username) => this.setState({ username })}
placeholder={'Username'}
style={styles.input}
/>
<TextInput
value={this.state.password}
onChangeText={(password) => this.setState({ password })}
placeholder={'Password'}
secureTextEntry={true}
style={styles.input}
/>
<View style={[{width:250}]}>
<Button
title={'Login'}
style={styles.btn}
onPress={this.onLogin.bind(this)}
/>
</View>
{loader && <Loader/>}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
input: {
width: 250,
height: 44,
padding: 10,
borderWidth: 1,
borderColor: 'black',
marginBottom: 5,
backgroundColor:'#fff'
},
heading:{
fontSize:24,
textAlign:'center',
marginBottom:5,
marginTop:0,
color:'#fff'
},
btn:{
display:'flex',
width: 250,
height: 44,
padding: 10,
borderWidth: 1,
borderColor: 'black',
marginTop: 50,
paddingLeft:75,
paddingRight:75,
backgroundColor:'#000'
},
main:{
backgroundColor:'#275faa',
flex:1,
display:'flex',
alignItems: 'center',
justifyContent: 'center',
alignItems: 'stretch',
}
});
// const styles = StyleSheet.create({
// container: {
// flex: 1,
// justifyContent: 'center',
// alignItems: 'center',
// backgroundColor: '#F5FCFF',
// },
// welcome: {
// fontSize: 20,
// textAlign: 'center',
// margin: 10,
// },
// instructions: {
// textAlign: 'center',
// color: '#333333',
// marginBottom: 5,
// },
// });
const ListStack = createStackNavigator({ List: ListScreen, UrlPage: WebviewScreen});
const AppStack = createStackNavigator({ Home: HomeScreen });
// const AuthStack = createStackNavigator({ SignIn: SignInScreen });
export default createAppContainer(createSwitchNavigator(
{
App: AppStack,
List: ListStack
}
));