Commit d19aa2e3 authored by pom421's avatar pom421
Browse files

refactor: withAuthentication with optionnal redirect

Layout can now be used even for a non authenticated user
parent d95a33fc
......@@ -40,33 +40,35 @@ const Header = ({ currentUser }) => {
<img src={"/images/logo.png"} alt="Logo" title="Logo"></img>
</NavbarBrand>
<NavbarToggler onClick={toggle} />
<Collapse isOpen={isOpen} navbar>
<Nav className="ml-auto d-flex align-items-center" navbar>
<NotificationsNoneIcon className="mr-2 medle-icons" width={"30px"} />
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav>
<AccountCircleIcon className="medle-icons" width={30}>
Mon compte&nbsp;
</AccountCircleIcon>
</DropdownToggle>
{currentUser && (
<Collapse isOpen={isOpen} navbar>
<Nav className="ml-auto d-flex align-items-center" navbar>
<NotificationsNoneIcon className="mr-2 medle-icons" width={"30px"} />
<UncontrolledDropdown nav inNavbar>
<DropdownToggle nav>
<AccountCircleIcon className="medle-icons" width={30}>
Mon compte&nbsp;
</AccountCircleIcon>
</DropdownToggle>
<DropdownMenu right>
{currentUser && (
<DropdownItem>{currentUser.firstName + " " + currentUser.lastName} </DropdownItem>
)}
<DropdownItem divider />
<Link href="/profile">
<a>
<DropdownItem>Profil</DropdownItem>
</a>
</Link>
<DropdownItem>Administration</DropdownItem>
<DropdownItem divider />
<DropdownItem onClick={logout}>Se déconnecter</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
</Collapse>
<DropdownMenu right>
{currentUser && (
<DropdownItem>{currentUser.firstName + " " + currentUser.lastName} </DropdownItem>
)}
<DropdownItem divider />
<Link href="/profile">
<a>
<DropdownItem>Profil</DropdownItem>
</a>
</Link>
<DropdownItem>Administration</DropdownItem>
<DropdownItem divider />
<DropdownItem onClick={logout}>Se déconnecter</DropdownItem>
</DropdownMenu>
</UncontrolledDropdown>
</Nav>
</Collapse>
)}{" "}
</Navbar>
<style jsx global>{`
.medle-icons {
......@@ -152,6 +154,11 @@ const Footer = () => (
<a href="mailto:contact.medle@fabrique.social.gouv.fr">Contactez-nous</a>
{/* </Link> */}
</li>
<li>
<Link href={"/faq"}>
<a>FAQ</a>
</Link>
</li>
</ul>
</Col>
</Row>
......@@ -175,6 +182,7 @@ const Footer = () => (
)
const Sidebar = ({ page, currentUser }) => {
if (!currentUser) return ""
return (
<>
<div className="list-group list-group-flush text-center">
......
......@@ -7,6 +7,7 @@ import { Container, Row } from "reactstrap"
import { Button, Title1 } from "../components/StyledComponents"
import Add from "@material-ui/icons/Add"
import { withAuthentication } from "../utils/auth"
import { ACT_MANAGEMENT } from "../utils/roles"
const ActConfirmationPage = ({ currentUser }) => {
const router = useRouter()
......@@ -47,4 +48,4 @@ ActConfirmationPage.propTypes = {
currentUser: PropTypes.object.isRequired,
}
export default withAuthentication(ActConfirmationPage)
export default withAuthentication(ActConfirmationPage, ACT_MANAGEMENT)
......@@ -105,7 +105,7 @@ const sessionTooOld = currentUser => {
return currentUser.authentifiedAt && moment(currentUser.authentifiedAt).add(timeout.session) < moment()
}
export const withAuthentication = (WrappedComponent, requiredPrivilege) => {
export const withAuthentication = (WrappedComponent, requiredPrivilege, { redirect = true } = {}) => {
const Wrapper = props => <WrappedComponent {...props} />
Wrapper.getInitialProps = async ctx => {
......@@ -113,14 +113,16 @@ export const withAuthentication = (WrappedComponent, requiredPrivilege) => {
logDebug("currentUser", currentUser)
if (!currentUser || sessionTooOld(currentUser)) {
logError("Pas de currentUser trouvé en cookie ou en SessionStorage. Redirection sur index")
isomorphicRedirect(ctx, "/index?sessionTimeout=1")
}
if (redirect) {
if (!currentUser || sessionTooOld(currentUser)) {
logError("Pas de currentUser trouvé en cookie ou en SessionStorage. Redirection sur index")
isomorphicRedirect(ctx, "/index?sessionTimeout=1")
}
if (requiredPrivilege && !isAllowed(currentUser.role, requiredPrivilege)) {
logError("Rôle incorrect. Redirection sur page permissionError")
isomorphicRedirect(ctx, "/permissionError")
if (requiredPrivilege && !isAllowed(currentUser.role, requiredPrivilege)) {
logError("Rôle incorrect. Redirection sur page permissionError")
isomorphicRedirect(ctx, "/permissionError")
}
}
const componentProps = WrappedComponent.getInitialProps && (await WrappedComponent.getInitialProps(ctx))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment