11import React , { Component } from "react" ;
22import Router from "./router" ;
33import "./App.css" ;
4-
54import { Provider } from "react-redux" ;
65import store from "./store" ;
76import jwt_decode from "jwt-decode" ;
87import { setAuthToken } from "./utils/setAuthToken" ;
98import { setCurrentUser , logoutUser } from "./actions/authAction" ;
109import "./css/main.scss" ;
1110
12- class App extends Component {
13- componentDidMount ( ) {
14- // check if user already loggedIn
15- const token = JSON . parse ( localStorage . getItem ( "jwtToken" ) ) ;
16- console . log ( "CHECKING TOKEN " , token ) ;
17- if ( token ) {
18- const decodedData = jwt_decode ( token ) ;
19- // set auth token in axios header
20- setAuthToken ( token ) ;
21- // set user in the state
22- setCurrentUser ( decodedData ) ;
23- // check if token is valid or expired
24- const currentTime = Date . now ( ) / 1000 ; // in ms
25- const expiryTime = decodedData . iat + 10800000 ; // 24 hrs
26- if ( expiryTime <= currentTime ) {
27- store . dispatch ( logoutUser ( ) ) ;
28- // now redirect to home page
29- window . location . href = "/" ;
30- }
31- }
11+ // check if user already loggedIn
12+ const token = localStorage . getItem ( "jwtToken" )
13+ console . log ( "CHECKING TOKEN " , token ) ;
14+ if ( token ) {
15+ const decodedData = jwt_decode ( token ) ;
16+ // set auth token in axios header
17+ setAuthToken ( token ) ;
18+ // set user in the state
19+ setCurrentUser ( decodedData ) ;
20+ // check if token is valid or expired
21+ const currentTime = Date . now ( ) / 1000 ; // in ms
22+ const expiryTime = decodedData . iat + 10800000 ; // 24 hrs
23+ if ( expiryTime <= currentTime ) {
24+ store . dispatch ( logoutUser ( ) ) ;
25+ window . location . href = "/"
3226 }
33- render ( ) {
27+ }
28+
29+ function App ( ) {
3430 return (
3531 < Provider store = { store } >
3632 < React . Fragment >
@@ -44,8 +40,7 @@ class App extends Component {
4440 </ React . Fragment >
4541 </ Provider >
4642 ) ;
47- }
4843}
49- document . title = "Donut" ;
5044
45+ document . title = "Donut"
5146export default App ;
0 commit comments