-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user authentication & admin authorization
- Loading branch information
1 parent
ee39471
commit 0f8c46f
Showing
37 changed files
with
5,240 additions
and
5,442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,64 @@ | ||
import React from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { useRouter } from 'next/router'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import Navbar from './navbar'; | ||
import Footer from './footer'; | ||
|
||
import Login from '../login-page/login'; | ||
|
||
const Layout: React.FunctionComponent = ({ children }) => { | ||
return ( | ||
<React.Fragment> | ||
<Navbar /> | ||
<main>{children}</main> | ||
<Footer /> | ||
</React.Fragment> | ||
); | ||
const [isProtectedRoute, setIsProtectedRoute] = useState(false); | ||
const [resetPasswordRoute, setResetPasswordRoute] = useState(false); | ||
const isAuthenticated = useSelector(state => state.auth.isAuthenticated); | ||
const router = useRouter(); | ||
console.log(router.pathname); | ||
useEffect(() => { | ||
if (router.pathname.includes('/reset-password') || router.pathname.includes('/verify-email')) { | ||
setIsProtectedRoute(() => false); | ||
setResetPasswordRoute(() => true); | ||
} else if (router.pathname === '/order' || router.pathname === '/cart' || router.pathname === '/') { | ||
setResetPasswordRoute(() => false); | ||
setIsProtectedRoute(() => true); | ||
} | ||
}, [router, isAuthenticated]); | ||
|
||
useEffect(() => { | ||
const redirectToLogin = () => { | ||
if (!isAuthenticated && isProtectedRoute) { | ||
return <Login />; | ||
} | ||
}; | ||
|
||
redirectToLogin(); | ||
}, [isAuthenticated]); | ||
|
||
let content; | ||
if (resetPasswordRoute) { | ||
content = ( | ||
<React.Fragment> | ||
<main>{children}</main> | ||
</React.Fragment> | ||
); | ||
} else if (!isAuthenticated && isProtectedRoute) { | ||
content = <Login />; | ||
} else if (!isAuthenticated && !isProtectedRoute) { | ||
content = ( | ||
<React.Fragment> | ||
<Login /> | ||
</React.Fragment> | ||
); | ||
} else { | ||
content = ( | ||
<React.Fragment> | ||
<Navbar /> | ||
<main>{children}</main> | ||
<Footer /> | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
return content; | ||
}; | ||
|
||
export default Layout; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.