Skip to content

Commit 04fc70b

Browse files
Fixes #80: Add guards so that only authenticated users can see the dashboard
1 parent 1f75ea3 commit 04fc70b

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

src/Routes.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,59 @@ import Register from './views/pages/register/Register';
1111
import CreateNewEvent from './components/CreateNewEvent';
1212
import Settings from './views/pages/settings/Settings';
1313
import LandingPage from './views/pages/HomeView/LandingPage';
14+
import AuthGuard from './components/auth/AuthGuard';
1415

1516
const renderRoutes = () => (
1617
<Suspense fallback={<LoadingScreen />}>
1718
<Switch>
1819
<Route path="/" exact render={() => <LandingPage />} />
1920
<Route path="/register" exact render={() => <Register />} />
20-
<Route path="/dashboard" exact render={() => <Navigation />} />
21-
<Route path="/profile" exact render={() => <Profile />} />
21+
<Route
22+
path="/dashboard"
23+
exact
24+
render={() => (
25+
<AuthGuard>
26+
<Navigation />
27+
</AuthGuard>
28+
)}
29+
/>
30+
<Route
31+
path="/profile"
32+
exact
33+
render={() => (
34+
<AuthGuard>
35+
<Profile />
36+
</AuthGuard>
37+
)}
38+
/>
2239
<Route path="/events" exact render={() => <EventDefaultPage />} />
23-
<Route path="/events/:eventID" exact render={() => <IndividualEvent />} />
24-
<Route path="/createEvent" exact render={() => <CreateNewEvent />} />
25-
<Route path="/settings" exact render={() => <Settings />} />
40+
<Route
41+
path="/events/:eventID"
42+
exact
43+
render={() => (
44+
<AuthGuard>
45+
<IndividualEvent />
46+
</AuthGuard>
47+
)}
48+
/>
49+
<Route
50+
path="/createEvent"
51+
exact
52+
render={() => (
53+
<AuthGuard>
54+
<CreateNewEvent />
55+
</AuthGuard>
56+
)}
57+
/>
58+
<Route
59+
path="/settings"
60+
exact
61+
render={() => (
62+
<AuthGuard>
63+
<Settings />
64+
</AuthGuard>
65+
)}
66+
/>
2667
<Route path="*" exact render={() => <Error404View />} />
2768
</Switch>
2869
</Suspense>

src/components/auth/Auth.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ function Auth({ children }) {
1515

1616
authService.handleAuthentication();
1717
authService.firebase.auth().onAuthStateChanged(user => {
18+
if (user) {
19+
const userItems = JSON.stringify({
20+
displayName: user.displayName,
21+
photoURL: user.photoURL,
22+
uid: user.uid
23+
});
24+
localStorage.setItem('causefolioUser', userItems);
25+
} else {
26+
localStorage.setItem('causefolioUser', user);
27+
}
1828
dispatch(setUserData(user));
1929
user.getIdToken().then(token => {
2030
authService.setSession(token);

src/components/auth/AuthGuard.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react';
2-
import { useSelector } from 'react-redux';
32
import { Redirect } from 'react-router-dom';
43
import PropTypes from 'prop-types';
4+
import { getLoggedUser } from 'src/services/authService';
55

66
function AuthGuard({ children }) {
7-
const account = useSelector(state => state.account);
7+
const user = getLoggedUser();
88

9-
if (!account.user) {
10-
return <Redirect to="/login" />;
9+
if (!user) {
10+
return <Redirect to="/" />;
1111
}
1212

1313
return children;

src/services/authService.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,7 @@ export const signInWithGoogle = () => {
122122
provider.setCustomParameters({ prompt: 'select_account' });
123123
return firebase.auth().signInWithPopup(provider);
124124
};
125+
126+
export const getLoggedUser = () => {
127+
return JSON.parse(localStorage.getItem('causefolioUser'));
128+
};

0 commit comments

Comments
 (0)