How to restrict access to routes in react-router?

后端 未结 7 1526
暗喜
暗喜 2020-12-07 08:49

Does anyone know how to restrict access to particular routes in react-router? I want to check if the user is logged in before allowing access to a particular route. I though

7条回答
  •  太阳男子
    2020-12-07 09:21

    usually a logged in user will be granted a token, and uses this token for any communication with server. What we usually do is define a root page, and things build on top of that page. this root page does localisation, authentication and other configurations for you.

    here's an example

    Routes = (
        
            
            
            
                
                
                
            
            
            
        
    );
    

    on your root page, check for token null or authenticate the token with server to see if user is valid login.

    hope this helps :)

提交回复
热议问题