-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
App.js
85 lines (79 loc) · 2.97 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, { useEffect } from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Navbar from './components/layout/Navbar';
import Landing from './components/layout/Landing';
import Register from './components/auth/Register';
import Login from './components/auth/Login';
import Alert from './components/layout/Alert';
import Dashboard from './components/dashboard/Dashboard';
import ProfileForm from './components/profile-forms/ProfileForm';
import AddExperience from './components/profile-forms/AddExperience';
import AddEducation from './components/profile-forms/AddEducation';
import Profiles from './components/profiles/Profiles';
import Profile from './components/profile/Profile';
import Posts from './components/posts/Posts';
import Post from './components/post/Post';
import NotFound from './components/layout/NotFound';
import PrivateRoute from './components/routing/PrivateRoute';
import { LOGOUT } from './actions/types';
// Redux
import { Provider } from 'react-redux';
import store from './store';
import { loadUser } from './actions/auth';
import setAuthToken from './utils/setAuthToken';
import './App.css';
const App = () => {
useEffect(() => {
// check for token in LS when app first runs
if (localStorage.token) {
// if there is a token set axios headers for all requests
setAuthToken(localStorage.token);
}
// try to fetch a user, if no token or invalid token we
// will get a 401 response from our API
store.dispatch(loadUser());
// log user out from all tabs if they log out in one tab
window.addEventListener('storage', () => {
if (!localStorage.token) store.dispatch({ type: LOGOUT });
});
}, []);
return (
<Provider store={store}>
<Router>
<Navbar />
<Alert />
<Routes>
<Route path="/" element={<Landing />} />
<Route path="register" element={<Register />} />
<Route path="login" element={<Login />} />
<Route path="profiles" element={<Profiles />} />
<Route path="profile/:id" element={<Profile />} />
<Route
path="dashboard"
element={<PrivateRoute component={Dashboard} />}
/>
<Route
path="create-profile"
element={<PrivateRoute component={ProfileForm} />}
/>
<Route
path="edit-profile"
element={<PrivateRoute component={ProfileForm} />}
/>
<Route
path="add-experience"
element={<PrivateRoute component={AddExperience} />}
/>
<Route
path="add-education"
element={<PrivateRoute component={AddEducation} />}
/>
<Route path="posts" element={<PrivateRoute component={Posts} />} />
<Route path="posts/:id" element={<PrivateRoute component={Post} />} />
<Route path="/*" element={<NotFound />} />
</Routes>
</Router>
</Provider>
);
};
export default App;