反应安全认证包装程序吗?

时间:2018-07-22 16:02:32

标签: reactjs firebase firebase-authentication

我目前在许多项目中都使用Firebase,对此我感到非常满意。但是,在使用Firebase登录功能开发“管理员仪表盘”时,遇到了一些相当奇怪的错误。您会看到,在我的登录页面中,代码是非常标准的,并且是这样的:

clickLogin(e){
    e.preventDefault();
    auth.signInWithEmailAndPassword(this.state.email, this.state.password).then((user) => {
        this.fillStore(user.uid);
    }).catch((error)=> {

        Alert.error('Wrong username or password.', {
            position: 'top-right',
            effect: 'jelly',
            timeout: 4000
        });

    })
}

对fillStore的调用只是一个函数,只需将两个字符串放在localStorage中,然后将用户推送到

this.props.history.push('/admin/welcome');

在“路线”中,我做了以下事情-试图为服务器上当前存在的所有管理页面创建包装:

const websiteRoutes = (
    <Router>
        <div>
            <Switch>
                <Route path="/" component={Frontpage} exact={true}/>
                <Route path=“/news” component={NewsPage} exact={true}/>
                <Route path=“/contact” component={ContactPage} exact={true}/>
                <Route path="/admin" component={AdminLogin} exact={true}/>
                <Authentication path="/admin/reset-passord" component={AdminResetPassword} exact={true}/>
                <Authentication path="/admin/settings” component={AdminSettings} exact={true}/>
            </Switch>
        </div>
    </Router>
)

最后但并非最不重要的-这是验证码:

const PrivateRoute = (props) => {
const user = auth.currentUser;

   if (user) {
      return <Route {...props} />
   } else {
    return <Redirect to='/admin'/>
   }
}

所以,我想在这里问一些经验丰富的程序员-这种解决方案安全吗?我想在Firebase的帮助下确保登录系统稳定。因为我注意到有时用户只是随机发送回登录名。似乎没有任何Firebase State Persistence无法正常工作。

另一个问题是,如何让用户一直保持登录状态,然后在关闭窗口后自动将其注销?

只是要清楚一点;该解决方案SEEMS可以“完美无瑕”地工作,只是偶尔会随机提示用户一次登录屏幕。

谢谢。

0 个答案:

没有答案