Check token earlier, load app faster

This commit is contained in:
Jake Howard 2016-05-05 18:25:00 +01:00
parent bad4e649e1
commit 87c6748037
2 changed files with 26 additions and 10 deletions

View file

@ -4,6 +4,8 @@ import RouteMaster from './routes/RouteMaster';
import RouteMapper from './navigation/RouteMapper'; import RouteMapper from './navigation/RouteMapper';
import GlobalStyles from '../settings/styles'; import GlobalStyles from '../settings/styles';
import BusyIndicator from 'react-native-busy-indicator'; import BusyIndicator from 'react-native-busy-indicator';
import token from '../api/token';
import { checkToken } from '../api/CircleCI';
require('moment-duration-format'); require('moment-duration-format');
@ -27,6 +29,13 @@ const styles = StyleSheet.create({
}); });
export default class extends React.Component { export default class extends React.Component {
constructor() {
super();
this.state = {
initialRoute: false
};
}
renderScene(route, nav) { renderScene(route, nav) {
const Component = route.component; const Component = route.component;
const props = route.props || {}; const props = route.props || {};
@ -41,15 +50,30 @@ export default class extends React.Component {
} }
componentWillMount() { componentWillMount() {
this.initialRoute = RouteMaster.get('LOGIN'); token.get().then(function (CIToken) {
if (!CIToken) {
this.setState({
initialRoute: RouteMaster.get('HOME')
});
return;
}
checkToken(CIToken).then(function (isValid) {
this.setState({
initialRoute: isValid ? RouteMaster.get('HOME') : RouteMaster.get('LOGIN')
});
}.bind(this));
}.bind(this));
} }
render() { render() {
if (!this.state.initialRoute) {
return null; // Replace with a splash screen?
}
return ( return (
<View style={{ flex: 1 }}> <View style={{ flex: 1 }}>
<Navigator <Navigator
renderScene={this.renderScene} renderScene={this.renderScene}
initialRoute={this.initialRoute} initialRoute={this.state.initialRoute}
navigationBar={ navigationBar={
<Navigator.NavigationBar <Navigator.NavigationBar
style={styles.navbar} style={styles.navbar}

View file

@ -163,14 +163,6 @@ export default class Login extends React.Component {
componentWillMount() { componentWillMount() {
BackAndroid.addEventListener('hardwareBackPress', () => { return true; }); // Disable back button BackAndroid.addEventListener('hardwareBackPress', () => { return true; }); // Disable back button
token.get().then(function (CIToken) {
if (!CIToken) {
return;
}
checkToken(CIToken).then(function (isValid) {
this.proceed();
}.bind(this));
}.bind(this));
} }
componentDidMount() { componentDidMount() {