Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PROTECTED_ROUTE --------------------------------------------------------
  2.  
  3. import React from 'react';
  4. import { Route, Redirect } from 'react-router-native';
  5. import { AsyncStorage } from 'react-native';
  6. import LayoutComponent from '../../layouts/AppLayout/index';
  7. import LoadingApp from '../LoadingApp';
  8. import Loading from '../Loading';
  9. import ImagePreloader from '../../../../utils/preloader';
  10. import Images from '../../../../assets/images/index';
  11.  
  12.  
  13. class ProtectedRoute extends React.Component {
  14.   constructor() {
  15.     super();
  16.  
  17.     this.state = {
  18.       ready: false,
  19.       token: null,
  20.     };
  21.  
  22.     this.checkToken = this.checkToken.bind(this);
  23.   }
  24.  
  25.   componentDidMount() {
  26.     ImagePreloader([Images.logo, Images.logoSquared, Images.logoIcon]);
  27.     this.checkToken();
  28.   }
  29.  
  30.   async checkToken() {
  31.     const self = this;
  32.     AsyncStorage.getItem('reactnativemeteor_usertoken').then((token) => {
  33.       setTimeout(() => {
  34.         self.setState({
  35.           ready: true,
  36.           token,
  37.         });
  38.       }, 1900);
  39.     });
  40.   }
  41.  
  42.   render() {
  43.     const {
  44.       component: Component, name, menu, back, footer, headTourIcon, headCommunityIcon, headInboxIcon, initialized, ...rest
  45.     } = this.props;
  46.     const { ready, token } = this.state;
  47.     console.log('READY', ready);
  48.     console.log('INITIALIZED', initialized);
  49.  
  50.     // console.log('headTourIcon_PROTECTED_ROUTE', headTourIcon);
  51.     if (!ready && !initialized) return <LoadingApp />;
  52.     if (!ready) return <Loading />;
  53.  
  54.     // Initialize Rendering function
  55.     const renderFunc = (props) => {
  56.       // Check for User Auth
  57.       if (token) {
  58.         // If user is Authenticated then continue
  59.         return (
  60.           <LayoutComponent title={name} footer={footer} headTourIcon={headTourIcon} headCommunityIcon={headCommunityIcon} headInboxIcon={headInboxIcon} menu={menu} back={back}>
  61.             <Component {...props} />
  62.           </LayoutComponent>
  63.         );
  64.       }
  65.  
  66.       // If user is not authenticated then redirect them to login
  67.  
  68.       return <Redirect to="/intro" />;
  69.     };
  70.  
  71.     // Return React Router's route with render function
  72.     return <Route render={renderFunc} {...rest} />;
  73.   }
  74. }
  75.  
  76. // Set Component Prop Types
  77. ProtectedRoute.propTypes = {};
  78. ProtectedRoute.defaultProps = {};
  79.  
  80. export default ProtectedRoute;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement