-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
63 lines (55 loc) · 1.42 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
import React, { Component } from 'react';
import { View } from 'react-native';
import SInfo from 'react-native-sensitive-info';
import ExternalStack from './routes/ExternalStack';
import InternalStack from './routes/InternalStack';
import Api from './utils/api';
const options = {
sharedPreferencesName: 'tallerRN',
keychainService: 'tallerRN'
};
export default class App extends Component {
state = {
isLoged: undefined
};
componentDidMount() {
const jwt = SInfo.getItem('jwt', options).then(value => {
// console.log(value);
this.setState({
isLoged: value ? true : false
});
});
}
login = useraccount => {
return Api.post('/user_token', useraccount)
.then(data => data.json())
.then(data => {
if (data && data.jwt) {
SInfo.setItem('jwt', data.jwt, options).then(() => {
this.setState({
isLoged: true
});
});
} else {
this.setState({
isLoged: false
});
}
});
};
logout = () => {
SInfo.deleteItem('jwt', options);
this.setState({
isLoged: false
});
};
render() {
if (this.state.isLoged) {
return <InternalStack screenProps={{ logout: this.logout }} />;
} else if (this.state.isLoged == undefined) {
return <View />;
} else {
return <ExternalStack screenProps={{ login: this.login }} />;
}
}
}