Skip to content

Commit 7ba58b0

Browse files
authoredNov 19, 2022
Feature: Protecte routes to be accessible after being loged in. (#116)
* Feature: Protecte routes to be accessible after being loged in.
1 parent c9a2a50 commit 7ba58b0

File tree

4 files changed

+74
-51
lines changed

4 files changed

+74
-51
lines changed
 

‎src/App.jsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Routes, Route, Outlet } from 'react-router-dom';
33
import { useTranslation } from 'react-i18next';
44
import { onAuthStateChanged } from 'firebase/auth';
55
import { useDispatch, useSelector } from 'react-redux';
6+
import { auth } from './services/firebaseConfig';
7+
import { login, logout, selectUser } from './features/user/userSlice';
68

79
import Home from './pages/Home';
810
import About from './pages/About';
@@ -18,8 +20,8 @@ import UserHome from './pages/UserHome';
1820
import Footer from './components/Footer';
1921
import Nav from './components/Navbar';
2022
import PublicProfile from './pages/PublicProfile';
21-
import { login, logout, selectUser } from './features/user/userSlice';
22-
import { auth } from './services/firebaseConfig';
23+
import ProtectedRoute from './components/ProtectedRoute'
24+
import FirebaseAuthContext from './context/FirebaseAuthContext';
2325

2426
const App = () => {
2527
const user = useSelector(selectUser);
@@ -44,6 +46,7 @@ const App = () => {
4446
}, []);
4547

4648
return (
49+
<FirebaseAuthContext>
4750
<Routes>
4851
<Route path="/" element={<Layout />}>
4952
<Route index element={user ? <UserHome /> : <Home />} />
@@ -52,13 +55,18 @@ const App = () => {
5255
<Route path="about" element={<About />} />
5356
<Route path="contact" element={<Contact />} />
5457
<Route path="team" element={<Team />} />
55-
<Route path="profile" element={<Profile />} />
56-
<Route path="library" element={<Library />} />
58+
59+
<Route element={<ProtectedRoute />}>
60+
<Route path="profile" element={<Profile />} exact/>
61+
<Route path="library" element={<Library />} exact/>
62+
</Route>
63+
5764
<Route path="review" element={<Review />} />
5865
<Route path="user/:id" element={<PublicProfile />} />
5966
<Route path="*" element={<NoMatch />} />
6067
</Route>
6168
</Routes>
69+
</FirebaseAuthContext>
6270
);
6371
};
6472

@@ -68,7 +76,7 @@ const Layout = () => {
6876

6977
return (
7078
<div>
71-
<Nav />
79+
<Nav />
7280
<Outlet />
7381
<Footer />
7482
</div>

‎src/components/ProtectedRoute.jsx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React, { useContext } from 'react';
2+
import { Outlet, Navigate } from 'react-router-dom';
3+
4+
import { AuthContext } from '../context/FirebaseAuthContext';
5+
6+
export default function ProtectedRoute() {
7+
const authValue = useContext(AuthContext);
8+
9+
if (authValue.userDataPresent) {
10+
if (authValue.user == null) {
11+
return <Navigate to="/login" />;
12+
}
13+
return <Outlet />;
14+
}
15+
return null;
16+
}

‎src/context/FirebaseAuthContext.jsx

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import React, { createContext, useState, useEffect } from 'react';
2+
import { auth, onAuthStateChanged } from '../services/firebaseConfig';
3+
4+
export const AuthContext = createContext({
5+
userDataPresent: false,
6+
user: null,
7+
});
8+
9+
export default function FirebaseAuthContext({ children }) {
10+
const [state, SetState] = useState({
11+
userDataPresent: false,
12+
user: null,
13+
listener: null,
14+
});
15+
16+
useEffect(() => {
17+
if (state.listener == null) {
18+
SetState({
19+
...state,
20+
listener: onAuthStateChanged(auth, (currentUser) => {
21+
if (currentUser)
22+
SetState((oldState) => ({
23+
...oldState,
24+
userDataPresent: true,
25+
user: currentUser,
26+
}));
27+
else
28+
SetState((oldState) => ({
29+
...oldState,
30+
userDataPresent: true,
31+
user: null,
32+
}));
33+
}),
34+
});
35+
}
36+
return () => {
37+
if (state.listener) state.listener();
38+
};
39+
}, []);
40+
41+
return <AuthContext.Provider value={state}>{children}</AuthContext.Provider>;
42+
}

‎yarn.lock

+3-46
Original file line numberDiff line numberDiff line change
@@ -3631,16 +3631,11 @@ caniuse-api@^3.0.0:
36313631
lodash.memoize "^4.1.2"
36323632
lodash.uniq "^4.5.0"
36333633

3634-
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001400:
3634+
caniuse-lite@^1.0.0, caniuse-lite@^1.0.30001400, caniuse-lite@^1.0.30001426:
36353635
version "1.0.30001429"
36363636
resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001429.tgz"
36373637
integrity sha512-511ThLu1hF+5RRRt0zYCf2U2yRr9GPF6m5y90SBCWsvSoYoW7yAGlv/elyPaNfvGCkp6kj/KFZWU0BMA69Prsg==
36383638

3639-
caniuse-lite@^1.0.30001426:
3640-
version "1.0.30001431"
3641-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001431.tgz#e7c59bd1bc518fae03a4656be442ce6c4887a795"
3642-
integrity sha512-zBUoFU0ZcxpvSt9IU66dXVT/3ctO1cy4y9cscs1szkPlcWb6pasYM144GqrUygUbT+k7cmUCW61cvskjcv0enQ==
3643-
36443639
case-sensitive-paths-webpack-plugin@^2.4.0:
36453640
version "2.4.0"
36463641
resolved "https://registry.npmjs.org/case-sensitive-paths-webpack-plugin/-/case-sensitive-paths-webpack-plugin-2.4.0.tgz"
@@ -8354,7 +8349,7 @@ postcss@^7.0.35:
83548349
picocolors "^0.2.1"
83558350
source-map "^0.6.1"
83568351

8357-
postcss@^8.3.5, postcss@^8.4.17, postcss@^8.4.4, postcss@^8.4.7:
8352+
postcss@^8.3.5, postcss@^8.4.17, postcss@^8.4.18, postcss@^8.4.4, postcss@^8.4.7:
83588353
version "8.4.18"
83598354
resolved "https://registry.npmjs.org/postcss/-/postcss-8.4.18.tgz"
83608355
integrity sha512-Wi8mWhncLJm11GATDaQKobXSNEYGUHeQLiQqDFG1qQ5UTDPTEvKw0Xt5NsTpktGTwLps3ByrWsBrG0rB8YQ9oA==
@@ -8363,15 +8358,6 @@ postcss@^8.3.5, postcss@^8.4.17, postcss@^8.4.4, postcss@^8.4.7:
83638358
picocolors "^1.0.0"
83648359
source-map-js "^1.0.2"
83658360

8366-
postcss@^8.4.18:
8367-
version "8.4.19"
8368-
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.19.tgz#61178e2add236b17351897c8bcc0b4c8ecab56fc"
8369-
integrity sha512-h+pbPsyhlYj6N2ozBmHhHrs9DzGmbaarbLvWipMRO7RLS+v4onj26MPFXA5OBYFxyqYhUJK456SwDcY9H2/zsA==
8370-
dependencies:
8371-
nanoid "^3.3.4"
8372-
picocolors "^1.0.0"
8373-
source-map-js "^1.0.2"
8374-
83758361
prelude-ls@^1.2.1:
83768362
version "1.2.1"
83778363
resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz"
@@ -9626,7 +9612,7 @@ symbol-tree@^3.2.4:
96269612
resolved "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz"
96279613
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==
96289614

9629-
tailwindcss@^3.0.2:
9615+
tailwindcss@^3.0.2, tailwindcss@^3.2.1:
96309616
version "3.2.1"
96319617
resolved "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.2.1.tgz"
96329618
integrity sha512-Uw+GVSxp5CM48krnjHObqoOwlCt5Qo6nw1jlCRwfGy68dSYb/LwS9ZFidYGRiM+w6rMawkZiu1mEMAsHYAfoLg==
@@ -9655,35 +9641,6 @@ tailwindcss@^3.0.2:
96559641
quick-lru "^5.1.1"
96569642
resolve "^1.22.1"
96579643

9658-
tailwindcss@^3.2.1:
9659-
version "3.2.2"
9660-
resolved "https://registry.yarnpkg.com/tailwindcss/-/tailwindcss-3.2.2.tgz#705f78cec8f4de2feb52abdb7a8a056e67f2d736"
9661-
integrity sha512-c2GtSdqg+harR4QeoTmex0Ngfg8IIHNeLQH5yr2B9uZbZR1Xt1rYbjWOWTcj3YLTZhrmZnPowoQDbSRFyZHQ5Q==
9662-
dependencies:
9663-
arg "^5.0.2"
9664-
chokidar "^3.5.3"
9665-
color-name "^1.1.4"
9666-
detective "^5.2.1"
9667-
didyoumean "^1.2.2"
9668-
dlv "^1.1.3"
9669-
fast-glob "^3.2.12"
9670-
glob-parent "^6.0.2"
9671-
is-glob "^4.0.3"
9672-
lilconfig "^2.0.6"
9673-
micromatch "^4.0.5"
9674-
normalize-path "^3.0.0"
9675-
object-hash "^3.0.0"
9676-
picocolors "^1.0.0"
9677-
postcss "^8.4.18"
9678-
postcss-import "^14.1.0"
9679-
postcss-js "^4.0.0"
9680-
postcss-load-config "^3.1.4"
9681-
postcss-nested "6.0.0"
9682-
postcss-selector-parser "^6.0.10"
9683-
postcss-value-parser "^4.2.0"
9684-
quick-lru "^5.1.1"
9685-
resolve "^1.22.1"
9686-
96879644
tapable@^1.0.0:
96889645
version "1.1.3"
96899646
resolved "https://registry.npmjs.org/tapable/-/tapable-1.1.3.tgz"

0 commit comments

Comments
 (0)
Please sign in to comment.