aboutsummaryrefslogtreecommitdiff
path: root/src/core/custom.router.js
blob: 9fcd4f6527b4ff05ef53b44b3dacbe4d0eec09d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import React from "react";
import { Route, Redirect } from "react-router-dom";
import Storage from "../services/storage.service";
import Permissions from "./permission.router";
import { RandomKey } from "../utils/random.key";

export const CustomRouter = ({ xComponent: Component, ...xProps }) => {
    return (
      <Route
        {...xProps}
        render={props => {
          let returnProps = {...xProps, ...props};
          let token = Storage.get("token");
          let pathName = props.match.path;
          if (!token && pathName !== "/login") {
            return <Redirect to="/login" />;
          } else if (pathName === "/login" && token) {
  
            return <Redirect to="/dashboard" />;
          }else if (pathName === "/" && token) {
  
            return <Redirect to="/dashboard" />;
          }
          return <Permissions {...returnProps} key={RandomKey.generate()} Component={Component}/>;
        }}
      />
    );
  };