-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
50 lines (45 loc) · 1.26 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
import React, { Component } from 'react';
import { View, ActivityIndicator, Text } from 'react-native';
import ExternalStack from './routes/ExternalStack';
import InternalStack from './routes/InternalStack';
import SInfo from 'react-native-sensitive-info';
import Api from './utils/api';
const options = {
sharedPreferencesName: 'tallerRN',
keychainService: 'tallerRN'
};
export default class App extends Component {
state = {
isLogued: null
}
componentDidMount() {
const jwt = SInfo.getItem('jwt', options).then(value => {
this.setState({
isLogued: value ? true : false,
});
});
}
login = (credentials) => {
Api.post('/user_token', credentials)
.then(res => res.json())
.then(data => {
if(data && data.jwt) {
SInfo.setItem('jwt', data.jwt, options).then(() => {
this.setState({ isLogued: true });
})
}
})
}
render() {
if (this.state.isLogued) {
return <InternalStack/>
} else if (this.state.isLogued == null) {
return <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<ActivityIndicator />
<Text>Cargando...</Text>
</View>
} else {
return <ExternalStack screenProps={{ login: this.login }}/>
}
}
}