🐛 Fixing cookie undefined & rule component
This commit is contained in:
parent
97729dc3cd
commit
5422efd530
@ -1,8 +1,8 @@
|
||||
import React, { useCallback, useEffect } from "react";
|
||||
import { useRouter } from "next/router";
|
||||
import Module from "@Front/Config/Module";
|
||||
import JwtService from "@Front/Services/JwtService/JwtService";
|
||||
import { IAppRule } from "@Front/Api/Entities/rule";
|
||||
import { useRouter } from "next/router";
|
||||
import Module from "@Front/Config/Module";
|
||||
|
||||
export enum RulesMode {
|
||||
OPTIONAL = "optional",
|
||||
@ -10,16 +10,18 @@ export enum RulesMode {
|
||||
}
|
||||
|
||||
type IProps = {
|
||||
isPage?: boolean;
|
||||
mode: RulesMode;
|
||||
rules: IAppRule[];
|
||||
no?: boolean;
|
||||
children: JSX.Element;
|
||||
isPage?: boolean;
|
||||
};
|
||||
|
||||
export default function Rules(props: IProps) {
|
||||
const router = useRouter();
|
||||
|
||||
const [isShowing, setIsShowing] = React.useState(false);
|
||||
const [hasJwt, setHasJwt] = React.useState(false);
|
||||
|
||||
const getShowValue = useCallback(() => {
|
||||
if (props.mode === RulesMode.NECESSARY) {
|
||||
return props.rules.every((rule) => JwtService.getInstance().hasRule(rule.name, rule.action));
|
||||
@ -27,19 +29,18 @@ export default function Rules(props: IProps) {
|
||||
return !!props.rules.find((rule) => JwtService.getInstance().hasRule(rule.name, rule.action));
|
||||
}, [props.mode, props.rules]);
|
||||
|
||||
const show = getShowValue();
|
||||
const [isShowing, setIsShowing] = React.useState(props.no ? !show : show);
|
||||
|
||||
useEffect(() => {
|
||||
setIsShowing(props.no ? !show : show);
|
||||
}, [props.no, show]);
|
||||
if (!JwtService.getInstance().decodeJwt()) return;
|
||||
setHasJwt(true);
|
||||
setIsShowing(getShowValue());
|
||||
}, [getShowValue, isShowing]);
|
||||
|
||||
if (!isShowing && props.isPage) {
|
||||
router.push(Module.getInstance().get().modules.pages.Home.props.path);
|
||||
}
|
||||
|
||||
if (!JwtService.getInstance().decodeJwt() || !isShowing) {
|
||||
if (props.isPage && !isShowing) {
|
||||
router.push(Module.getInstance().get().modules.pages[404].props.path);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!hasJwt || !isShowing) return null;
|
||||
|
||||
return props.children;
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ import { useCallback, useState } from "react";
|
||||
|
||||
import classes from "./classes.module.scss";
|
||||
import JwtService from "@Front/Services/JwtService/JwtService";
|
||||
import Rules, { RulesMode } from "@Front/Components/Elements/Rules";
|
||||
import { AppRuleActions, AppRuleNames } from "@Front/Api/Entities/rule";
|
||||
|
||||
type IProps = {};
|
||||
export default function RolesCreate(props: IProps) {
|
||||
@ -61,6 +63,14 @@ export default function RolesCreate(props: IProps) {
|
||||
}, [hasChanged, redirect]);
|
||||
|
||||
return (
|
||||
<Rules
|
||||
mode={RulesMode.NECESSARY}
|
||||
rules={[
|
||||
{
|
||||
action: AppRuleActions.create,
|
||||
name: AppRuleNames.officeRoles,
|
||||
},
|
||||
]}>
|
||||
<DefaultRolesDashboard mobileBackText={"Liste des rôles"} hasBackArrow title="Créer un rôle">
|
||||
<div className={classes["root"]}>
|
||||
<div className={classes["header"]}>
|
||||
@ -91,5 +101,6 @@ export default function RolesCreate(props: IProps) {
|
||||
</Confirm>
|
||||
</div>
|
||||
</DefaultRolesDashboard>
|
||||
</Rules>
|
||||
);
|
||||
}
|
||||
|
@ -68,10 +68,8 @@ export default class JwtService {
|
||||
}
|
||||
|
||||
public hasRule(name: string, action: string) {
|
||||
const accessToken = CookieService.getInstance().getCookie("leCoffreAccessToken");
|
||||
if (!accessToken) return false;
|
||||
const decodedToken = this.decodeJwt();
|
||||
if (!decodedToken) return false;
|
||||
return decodedToken?.rules?.some((rule: string) => rule === `${action} ${name}`);
|
||||
const token = this.decodeJwt();
|
||||
if (!token) return false;
|
||||
return token?.rules?.some((rule: string) => rule === `${action} ${name}`);
|
||||
}
|
||||
}
|
||||
|
@ -25,22 +25,6 @@ export async function middleware(request: NextRequest) {
|
||||
return NextResponse.redirect(new URL("/login", request.url));
|
||||
}
|
||||
|
||||
const requestUrlPath = request.nextUrl.pathname;
|
||||
if (
|
||||
requestUrlPath.startsWith("/collaborators") ||
|
||||
requestUrlPath.startsWith("/deed-types") ||
|
||||
requestUrlPath.startsWith("/customer") ||
|
||||
requestUrlPath.startsWith("/offices") ||
|
||||
requestUrlPath.startsWith("/roles") ||
|
||||
requestUrlPath.startsWith("/users")
|
||||
) {
|
||||
if (userDecodedToken.role !== "admin" && userDecodedToken.role !== "super-admin")
|
||||
return NextResponse.redirect(new URL("/404", request.url));
|
||||
}
|
||||
if ((requestUrlPath.startsWith("/my-account") || requestUrlPath.startsWith("/document-types")) && !userDecodedToken)
|
||||
return NextResponse.redirect(new URL("/404", request.url));
|
||||
if (requestUrlPath.startsWith("/client-dashboard") && !customerDecodedToken) return NextResponse.redirect(new URL("/404", request.url));
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user