{"size": 1166, "ext": "js", "max_stars_repo_path": "ClientApp/components/Security/PrivateRoute.js", "max_stars_repo_name": "datrax/React-Template", "max_stars_repo_stars_event_min_datetime": "2018-10-05T16:47:47.000Z", "max_stars_repo_stars_event_max_datetime": "2018-10-05T16:47:47.000Z", "max_issues_repo_path": "ClientApp/components/Security/PrivateRoute.js", "max_issues_repo_name": "datrax/React-Template", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "ClientApp/components/Security/PrivateRoute.js", "max_forks_repo_name": "datrax/React-Template", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.5, "max_line_length": 101, "alphanum_fraction": 0.4794168096}
﻿import React, { Component } from "react";
import { BrowserRouter, Redirect, Route } from "react-router-dom";
import axios from 'axios';
import Async from 'react-promise'

const PrivateRoute = ({ layout: Layout, component: Component, ...rest }) => {



    return (
        
            <Route
                {...rest}
                render={props =>
                    <Layout> <Async promise={CheckIfLoggedIn()} then={(val) => val ? (
                        <Component {...props} />  

                    ) : (
                            <Redirect to={{ pathname: '/login', state: { from: props.location } }} />
                        )} /> </Layout>
                }
            />
     
    );
}
function CheckIfLoggedIn() {
    console.log('login contr');
    var token = window.localStorage.getItem('token');
    if (!token) {
        return Promise.resolve(false);
    }

    return axios.get('/validateToken')
        .then(function (response) {
            return true;
        })
        .catch(function (error) {

            if (error.response.status == 401) {
                return false;
            }
        });
}
export default PrivateRoute