-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.js
104 lines (93 loc) · 2.37 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
100
101
102
103
104
/*jshint esversion: 6 */
import React, { Component } from 'react';
import {
StyleSheet,
TouchableHighlight,
AsyncStorage,
Text,
View,
Image
} from 'react-native';
import styles from './styles';
import constants from './constants'
class Root extends Component {
constructor(props) {
super(props);
//console.log("constructor")
this.state = {
isLoggedIn: true
}
}
componentDidMount() {
this.getToken();
}
navigate(routeName) {
this.props.navigator.push({
name: routeName
});
}
async getToken() {
try {
let accessToken = await AsyncStorage.getItem(constants.ACCESS_TOKEN);
if(!accessToken) {
this.setState({isLoggedIn: false})
} 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://seekerdnasecure.co.za:3002/token', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({id_token: token})
});
if (response.status >= 200 && response.status < 300) {
//console.log("succhess");
this.navigate('home');
} else {
this.setState({isLoggedIn: false});
//Handle error
}
} catch(error) {
this.setState({isLoggedIn: false});
}
}
render() {
if (this.state.isLoggedIn === true) {
return (
<View style={styles.container}>
<Image
source={require('./img/logo.png')}
style={styles.logo}
/>
</View>
);
}
else {
return (
<View style={styles.container}>
<Image
source={require('./img/logo.png')}
style={styles.logo}
/>
<TouchableHighlight onPress={ this.navigate.bind(this, 'login') } style={styles.button}>
<Text style={styles.buttonText}>Login</Text>
</TouchableHighlight>
<TouchableHighlight onPress={ this.navigate.bind(this,'register') } style={styles.button}>
<Text style={styles.buttonText}>Register</Text>
</TouchableHighlight>
</View>
);
}
}
}
export default Root