forked from michaelwoon/R2S2-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormGenerator.js
115 lines (106 loc) · 2.64 KB
/
FormGenerator.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
import React, { Component } from 'react';
import { AppRegistry, Alert } from 'react-native';
import { View, Text, Button } from 'native-base';
import GenerateForm from 'react-native-form-builder';
import base64 from "react-native-base64";
const fields = [
{
type: 'text',
name: 'user_name',
required: true,
icon: 'ios-person',
label: 'Username',
},
{
type: 'password',
name: 'password',
icon: 'ios-lock',
required: true,
label: 'Password',
},
];
export default class FormGenerator extends Component {
static navigationOptions = {
title: 'Login',
};
constructor(props) {
super(props);
global.token = '';
};
login() {
const formValues = this.formGenerator.getValues();
//set props with username and pass (hardcoded for now)
// const username = formValues.user_name;
// const pass = formValues.password;
const username = '[email protected]';
const pass = 'password';
let headers = new Headers()
headers.append(
"Authorization",
"Basic " +
base64.encode(
username.toLowerCase() + ":" + pass
)
);
fetch("http://35.194.88.251/api/login", {
method: "GET",
headers: headers
})
.then(
function(response) {
if (response.status == 200) {
response.json().then(
function(data) {
Alert.alert(
'Login Successful!'
);
global.token = data.token;
console.log(global.token);
this.props.navigation.navigate('Home');
}.bind(this)
);
} else {
Alert.alert(
'Invalid username or password'
);
}
}.bind(this)
)
.catch(function(ex) {
Alert.alert(
'Cannot connect to network'
);
console.log('CATCH',ex);
});
}
render() {
return (
<View style={styles.wrapper}>
<View>
<GenerateForm
ref={(c) => {
this.formGenerator = c;
}}
fields={fields}
/>
</View>
<View style={styles.submitButton}>
<Button block onPress={() => this.login()}>
<Text>Login</Text>
</Button>
</View>
</View>
);
}
}
const styles = {
wrapper: {
flex: 1,
marginTop: 150,
},
submitButton: {
paddingHorizontal: 10,
paddingTop: 20,
},
};
AppRegistry.registerComponent('FormGenerator', () => FormGenerator);