{"size": 2821, "ext": "js", "max_stars_repo_path": "src/App.js", "max_stars_repo_name": "PratikSavla/CREDucate", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/App.js", "max_issues_repo_name": "PratikSavla/CREDucate", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/App.js", "max_forks_repo_name": "PratikSavla/CREDucate", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.0, "max_line_length": 134, "alphanum_fraction": 0.606876994}
import React, {useEffect, useContext} from 'react';
import ApiService from './utils/apiService';
import Navbar from './components/layout/Navbar';
import { apiInstances } from './utils/api';
import jwt_decode from 'jwt-decode';
import Router from './components/Router';
import { AppContext,defaultAppState } from './utils/context';
import M from 'materialize-css'

function App() {
  const [appState, setAppState] = useContext(AppContext)

  useEffect(() => {
    const accessToken = ApiService.getAccessTokenFromLocalStorage();
    const didToken = ApiService.getDidTokenToLocalStorage();
    const user = ApiService.getUserToLocalStorage();

    if( accessToken ) {
      const jwtToken = jwt_decode(accessToken);
      /**
       * On component mount, check if user access token is valid.
       * If not, log out user on client side.
       * */
      if( (jwtToken.exp * 1000) <= new Date().getTime() ) {

        ApiService.removeAccessAndDidTokens()

        M.toast({html : 'Your JWT token has expired. Please, log in again.', displayLength : 1000})

        setAppState(prevState => {
          return {
            ...prevState,
            isAuthenticated: false,
          }
        })

        return;
      }
      else {
        /**
         * Log in user on client side, set authorization bearer token.
         * */
        const userState = jwt_decode(user);
        ApiService.setAuthorizationBearer(accessToken);
        setAppState(prevState => {
          return {
            ...prevState,
            didToken: didToken || null,
            accessToken,
            isAuthenticated: true,
            username: jwt_decode(accessToken).username,
            ...userState
          }
        })
      }
    }

    apiInstances.forEach(instance => {
      /**
       * In case of 401 HTTP responses, remove tokens from localstorage
       * and reset app context state (basically, log out user on client side).
       * */
      instance.interceptors.response.use(function (response) {
        return response;
      }, function (error) {
        if (error.response && 401 === error.response.status) {
          ApiService.removeAccessAndDidTokens()

          setAppState({
            ...appState,
            ...defaultAppState
          })

          M.toast({html : 'Your JWT token has expired. Please, log in again.', displayLength : 1000})
          return Promise.reject(error);
        } else {
          return Promise.reject(error);
        }
      });
    })
  }, []);// eslint-disable-line react-hooks/exhaustive-deps

  return (
    <div className="">
      <Navbar isUserAuthenticated={appState.isAuthenticated}/>
      <Router isUserAuthenticated={appState.isAuthenticated} isInstitution={appState.isInstitution} isVerifier={appState.isVerifier}/>
    </div>
  )
}

export default App;