forked from itzikbenh/React-Native-on-Rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.js
99 lines (91 loc) · 2.28 KB
/
root.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
'use strict';
import React, {
Component,
StyleSheet,
TouchableHighlight,
AsyncStorage,
Text,
View
} from 'react-native';
const ACCESS_TOKEN = 'access_token';
class Root extends Component {
componentWillMount() {
this.getToken();
}
navigate(routeName) {
this.props.navigator.push({
name: routeName
});
}
async getToken() {
try {
let accessToken = await AsyncStorage.getItem(ACCESS_TOKEN);
if(!accessToken) {
console.log("Token not set");
} else {
this.verifyToken(accessToken)
}
} catch(error) {
console.log("Something went wrong");
}
}
//If token is verified we will redirect the user to the home page
async verifyToken(token) {
let accessToken = token
try {
let response = await fetch('https://afternoon-beyond-22141.herokuapp.com/api/verify?session%5Baccess_token%5D='+accessToken);
let res = await response.text();
if (response.status >= 200 && response.status < 300) {
//Verified token means user is loggen in to we redirect to home.
this.navigate('home');
} else {
//Handle error
let error = res;
throw error;
}
} catch(error) {
console.log("error response: " + error);
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.title}>Welcome Friend </Text>
<TouchableHighlight onPress={ this.navigate.bind(this,'register') } style={styles.button}>
<Text style={styles.buttonText}>Register</Text>
</TouchableHighlight>
<TouchableHighlight onPress={ this.navigate.bind(this, 'login') } style={styles.button}>
<Text style={styles.buttonText}>Login</Text>
</TouchableHighlight>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: '#F5FCFF',
padding: 10,
paddingTop: 180
},
button: {
height: 50,
backgroundColor: '#48BBEC',
alignSelf: 'stretch',
alignItems: 'center',
marginTop: 10,
justifyContent: 'center'
},
buttonText: {
fontSize: 22,
color: '#FFF',
alignSelf: 'center'
},
title: {
fontSize: 25,
marginBottom: 15
}
});
export default Root