UNPKG

@brightlayer-ui/react-auth-workflow

Version:

Re-usable workflow components for Authentication and Registration within Eaton applications.

21 lines (20 loc) 900 B
import React from 'react'; import { Navigate, useLocation } from 'react-router-dom'; /** * Component that renders a conditional Route. If the user is not authenticated, this route * will redirect to the route specified by user using fallBackUrl prop. If the user is authenticated, it will render the provided * content. * @param children The element/route to render if the user is authenticated * @param isAuthenticated Indicates whether the user is authenticated or not * @param fallBackUrl URL where to redirect if user is not authenticated * * @category Component */ export const ReactRouterAuthGuard = (props) => { const { children = null, fallBackUrl, isAuthenticated } = props; const location = useLocation(); if (!isAuthenticated) { return React.createElement(Navigate, { to: fallBackUrl, replace: true, state: { from: location } }); } return children; };