Skip to content

Commit 0f8c46f

Browse files
committed
feat: add user authentication & admin authorization
1 parent ee39471 commit 0f8c46f

37 files changed

+5240
-5442
lines changed

components/layout/layout.tsx

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
2+
import { useRouter } from 'next/router';
3+
import { useSelector } from 'react-redux';
24

35
import Navbar from './navbar';
46
import Footer from './footer';
57

8+
import Login from '../login-page/login';
9+
610
const Layout: React.FunctionComponent = ({ children }) => {
7-
return (
8-
<React.Fragment>
9-
<Navbar />
10-
<main>{children}</main>
11-
<Footer />
12-
</React.Fragment>
13-
);
11+
const [isProtectedRoute, setIsProtectedRoute] = useState(false);
12+
const [resetPasswordRoute, setResetPasswordRoute] = useState(false);
13+
const isAuthenticated = useSelector(state => state.auth.isAuthenticated);
14+
const router = useRouter();
15+
console.log(router.pathname);
16+
useEffect(() => {
17+
if (router.pathname.includes('/reset-password') || router.pathname.includes('/verify-email')) {
18+
setIsProtectedRoute(() => false);
19+
setResetPasswordRoute(() => true);
20+
} else if (router.pathname === '/order' || router.pathname === '/cart' || router.pathname === '/') {
21+
setResetPasswordRoute(() => false);
22+
setIsProtectedRoute(() => true);
23+
}
24+
}, [router, isAuthenticated]);
25+
26+
useEffect(() => {
27+
const redirectToLogin = () => {
28+
if (!isAuthenticated && isProtectedRoute) {
29+
return <Login />;
30+
}
31+
};
32+
33+
redirectToLogin();
34+
}, [isAuthenticated]);
35+
36+
let content;
37+
if (resetPasswordRoute) {
38+
content = (
39+
<React.Fragment>
40+
<main>{children}</main>
41+
</React.Fragment>
42+
);
43+
} else if (!isAuthenticated && isProtectedRoute) {
44+
content = <Login />;
45+
} else if (!isAuthenticated && !isProtectedRoute) {
46+
content = (
47+
<React.Fragment>
48+
<Login />
49+
</React.Fragment>
50+
);
51+
} else {
52+
content = (
53+
<React.Fragment>
54+
<Navbar />
55+
<main>{children}</main>
56+
<Footer />
57+
</React.Fragment>
58+
);
59+
}
60+
61+
return content;
1462
};
1563

1664
export default Layout;

components/layout/navbar.tsx

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import Link from 'next/link';
2-
import React, { useEffect, useState } from 'react';
2+
import React from 'react';
33

4+
import { connect } from 'react-redux';
5+
import { removeAuthenticatedUser } from '../../redux/actions/index';
6+
import { ReducerType } from '../../redux/reducers/rootReducer';
47
import { removedUserFromLocalStorage } from '../../utils/functions/helpers';
58

6-
const Navbar: React.FunctionComponent = () => {
7-
const [user, setUser] = useState<any>(false);
8-
9-
useEffect(() => {
10-
if (typeof window !== 'undefined') {
11-
setUser(() => localStorage.getItem('user'));
12-
}
13-
}, [user]);
9+
const Navbar: React.FunctionComponent = (props: any) => {
10+
const { isAuthenticated, isADmin } = props.auth;
1411

1512
const signedOutHandler = () => {
13+
props.removeAuthenticatedUser();
1614
removedUserFromLocalStorage();
17-
setUser(false);
1815
};
1916

2017
return (
@@ -30,31 +27,36 @@ const Navbar: React.FunctionComponent = () => {
3027
<Link href="/cart">
3128
<a className="customlink">Cart</a>
3229
</Link>
33-
<Link href="/add-product">
34-
<a className="customlink">Add Product</a>
35-
</Link>
30+
{isADmin === 'admin' && (
31+
<Link href="/add-product">
32+
<a className="customlink">Add Product</a>
33+
</Link>
34+
)}
3635
<Link href="/order">
3736
<a className="customlink">Oder</a>
3837
</Link>
39-
<Link href="/admin/products">
40-
<a className="customlink">Admin Products</a>
41-
</Link>
42-
<Link href="/admin/users-ui">
43-
<a className="customlink"> Admin Users UI</a>
44-
</Link>
45-
<Link href="/admin/users-table">
46-
<a className="customlink"> Admin Users Table</a>
47-
</Link>
38+
{isADmin === 'admin' && (
39+
<>
40+
<Link href="/admin/products">
41+
<a className="customlink">Admin Products</a>
42+
</Link>
43+
<Link href="/admin/users-ui">
44+
<a className="customlink"> Admin Users UI</a>
45+
</Link>
46+
<Link href="/admin/users-table">
47+
<a className="customlink"> Admin Users Table</a>
48+
</Link>
49+
</>
50+
)}
4851
</div>
49-
{user && (
52+
{isAuthenticated && (
5053
<div>
5154
<button className=" customlink" onClick={signedOutHandler}>
5255
SignOut
5356
</button>
5457
</div>
5558
)}
56-
57-
{!user && (
59+
{!isAuthenticated && (
5860
<div className=" flex justify-between items-center space-x-8">
5961
<Link href="/login">
6062
<a className="customlink cursor-pointer">Login</a>
@@ -70,4 +72,17 @@ const Navbar: React.FunctionComponent = () => {
7072
);
7173
};
7274

73-
export default Navbar;
75+
const mapStateToProps = (state: ReducerType) => {
76+
return {
77+
auth: state.auth
78+
};
79+
};
80+
81+
const mapDispatchToProps = {
82+
removeAuthenticatedUser
83+
};
84+
85+
export default connect(
86+
mapStateToProps,
87+
mapDispatchToProps
88+
)(Navbar);

0 commit comments

Comments
 (0)