简体   繁体   English

如何在React.JS中有条件地添加路由?

[英]How to conditionally add routes in React.JS?

I have a login system with Firebase and I want to dynamically add routes if someone enters correct information, I want it to add the main route and the add route. 我有一个使用Firebase的登录系统,如果有人输入正确的信息,我想动态添加路线,我希望它添加主路线和添加路线。

var userToken = 'firebase:authUser:AIzaSyDfC9LqfL1rbClhfCOD04BKC9ZIn7UAZ_g:[DEFAULT]';

ReactDOM.render(
    <Router history={hashHistory}>
        <Route path="/" component={Login} />
        <Route path="signup" component={Signup} />
         { localStorage.getItem(userToken) ? (
            <Route path="main" component={Main} />
          ) : (<Redirect from="main" to="/" />)
        }
        { localStorage.getItem(userToken) ? (
            <Route path="add" component={Add} />
          ) : null
        }  
        <Route path="main" component={Main} />
        <Route path="*" component={NotFound} />
    </Router>
    , document.getElementById('container'));

The code above is working fine, but it has a flaw when someone types the correct information, they have to reload the page and enter the information again to log in. 上面的代码工作正常,但是当有人输入正确的信息时,它有一个缺陷,他们必须重新加载页面并再次输入信息才能登录。

Is there any way around where they don't have to reload the page after successful authentication? 有成功的身份验证后,他们不必重新加载页面吗?

I'd suggest creating components SecureMain and SecureAdd and updating the routes to use those instead. 我建议创建组件SecureMainSecureAdd并更新路由以使用它们。 These would check for the existence of the user token and render either the desired content, or your NotFound component otherwise. 这些将检查是否存在用户令牌并呈现所需内容,否则呈现NotFound组件。 For example (note that I banged this out as a concept and it may not be perfect): 例如(请注意,我将其作为一个概念进行了抨击,它可能并不完美):

import React from 'react';
import Main from './Main';
import NotFound from './NotFound';

export default class SecureMain extends React.Component {
  constructor(...args) {
    super(...args);
    this.state = { token: localStorage.getItem(userToken) };
  }
  render() {
    if (this.state.token) return <Main {...this.props} />;
    else return <NotFound {...this.props} />;
  }
}

This makes the state of your application much easier to predict based on its state. 这使得应用程序的状态更容易根据其状态进行预测。

Of course I also have to echo @ZekeDroid 's disclaimer about client-side "security": 当然,我还要回应@ZekeDroid关于客户端“安全性”的免责声明:

客户端安全IRL

That's an interesting approach at authentication. 这是一种有趣的身份验证方法。 Very interesting. 很有意思。 My suggestion is for you to add Redux or straight up to wrap your Router in a parent component. 我的建议是你添加Redux或直接将你的Router包装在父组件中。 This way, when the user triggers what I assume is some onChange handler, it should update that parent's state rather than localStorage only. 这样,当用户触发我假设的一些onChange处理程序时,它应该只更新父级的状态而不是localStorage At that point, that parent will re-render its children (in your case, the Router ) hence generating new Routes ! 此时,该父级将重新呈现其子级(在您的情况下, Router ),从而生成新的Routes

Disclaimer: it's still easy to get around this authentication since anyone can go to the source and change the line to read { true ? <Route path="add" component={Add} /> : null } 免责声明:由于任何人都可以访问源代码并将行更改为{ true ? <Route path="add" component={Add} /> : null } { true ? <Route path="add" component={Add} /> : null } , but still, kinda cool approach. { true ? <Route path="add" component={Add} /> : null } ,但仍然是一种很酷的方法。

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

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