简体   繁体   English

React Router 不访问受保护的路由

[英]React Router doesn't access Protected Route

In my typescript react project i have created a protected route to check for authenticationbefore rendering a component.在我的 typescript React 项目中,我创建了一个受保护的路由来在渲染组件之前检查身份验证。

export const ProtectedRoute: React.FC<ProtectedRouteProps> = props =>{

    const currentLocation = useLocation();

    let redirectpath = props.redirectPathOnAuthentication;
    console.log(redirectpath)
    console.log('in protected route')
    if(!props.isAuthenticated){
        props.setRedirectPathOnAuthentication(currentLocation.pathname);
        redirectpath = props.authenticationPath;
    }

        if(redirectpath !== currentLocation.pathname){
            const renderComponent = () => <Redirect to={{pathname: redirectpath}} />;
            return <Route {...props} component={renderComponent} render={undefined} />
        }else{
            return <Route {...props} />
        }
    }

I pass in the props to make conditional rendering based, on rather the user is authenticated or not.我传入道具以根据用户是否经过身份验证进行条件渲染。 I have the tries to access a ProtectedRoute, the user will be redirected to the route where login is possible ( named as /home route), and then redirected back to the original route.我尝试访问 ProtectedRoute,用户将被重定向到可以登录的路由(命名为/home路由),然后重定向回原始路由。

   
export const RoutingHandler: React.FC = (props: Props) => {
    
    const location = useLocation()
    
    const [sessionContext, updateSessionContext] = useSessionContext()
    console.log(location)

    const setRedirectPathOnAuthentication = (path: string) =>{
        updateSessionContext({...sessionContext, redirectPathOnAuthentication: path})
    }

    const defaultProtectedRouteProps: ProtectedRouteProps = {
        isAuthenticated: !!sessionContext.isAuthenticated,
        authenticationPath: '/home',
        redirectPathOnAuthentication: sessionContext.redirectPathOnAuthentication || '',
        setRedirectPathOnAuthentication
    }

    console.log(defaultProtectedRouteProps)

    return (
        <div>
        <Navbar />
        <Switch>
        <ProtectedRoute {...defaultProtectedRouteProps} path="/dashboard" component={Dashboard} />
        <Route exact path="/home" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/help" component={Help} />
        <Redirect from="/" to="/home" />
        </Switch>
        </div>
    )
}

Whenever I do `history.push('/dashboard');` the `Dashboard` component is never rendered.

For desired implementation, you can try this approach, that works fine:对于所需的实现,您可以尝试这种工作正常的方法:

export const PrivateRoute = ({component: Component, isAuthenticated, ...rest}) => (
    <Route {...rest} 
        render={
            props => (
                isAuthenticated ? (
                    <Component {...props} />
                ):
                (
                    <Redirect to={{ pathname: '/login', state: { from: props.location }}}/>
                )
            )
        }
    />
)

PrivateRoute.propTypes = {
    isAuthenticated: PropTypes.bool.isRequired
}

const mapStateToProps = state => ({
  isAuthenticated: state.auth.isAuthenticated
});


export default connect(mapStateToProps)(PrivateRoute)

so, in DefaultsRoutes :所以,在DefaultsRoutes中:

<Switch>
     <Route path="/login" component={Login} />
     <Route path="/signup" component={Signup} />
     <PrivateRoute component={Home} />
     <Route path="*" component={NotFound404} />
</Switch>

in App :App中:


let AppRedirect = ({ loadingApp }) => {
  return loadingApp ? <Loading /> : <DefaultRoutes />;
};

const mapState = (state) => ({
  loadingApp: state.app.loadingApp,
});

AppRedirect = connect(mapState)(AppRedirect);

export class App extends React.Component {

  render() {
    return (
      <Provider store={store}>
          <AppRedirect />
      </Provider>
    );
  }
}

export default App;

When user successfully logged in dispatch an action to change state: loadingApp: true -> loadingApp: false当用户成功登录时,发送一个动作来改变 state: loadingApp: true -> loadingApp: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM