【问题标题】:Why Firebase user is not authenticated anymore when refreshing?为什么 Firebase 用户在刷新时不再经过身份验证?
【发布时间】:2018-07-09 20:09:09
【问题描述】:

一个非常简单的应用程序。在/ 路径上,我想在登录时呈现Home,如果未登录,则呈现Introduction。在登录页面上登录有效,如果我 console.log(firebase.auth().currentUser) 它显示所有数据和/ 正确呈现Home。因此,虽然仍在/ 路径上,但发生了一些奇怪的事情;当我刷新页面时,我们不再登录,它呈现Introduction。但是当我单击链接<li><Link to='/'>Home</Link></li> 时,Home 组件会正确呈现,并且我们会再次登录。为什么会这样?

import React, { Component } from 'react';
import {BrowserRouter as Router, Route, Switch, Link, Redirect} from 'react-router-dom';
import firebase from './firebase'

import Home from './containers/home'
import Login from './containers/login'
import Register from './containers/register'
import Dashboard from './containers/dashboard'

const Protected = ({component: Component, ...rest}) => (
  <Route {...rest} render={(props) => {
    if(firebase.auth().currentUser) 
      return <Component {...props} />
     else 
      return <Redirect to='/register' />

  }} />
)

const Introduction = props => ( <h1>hello</h1>)

class App extends Component {


  render() {
    return (
      <Router>
        <React.Fragment>
          <ul>
            <li><Link to='/'>Home</Link></li>
            <li><Link to='/register'>Register</Link></li>
            <li><Link to='/login'>login</Link></li>
            <li><Link to='/dashboard'>dashboard</Link></li>
          </ul>
          <Switch>
            <Route path='/' exact render={(props) => {
              if(firebase.auth().currentUser) 
                return <Home {...props} />
              else
                return <Introduction {...props} />
              }
            } />
            <Route path='/register' exact component={Register} />
            <Route path='/login' component={Login} />
            <Protected path='/dashboard' component={Dashboard} />
          </Switch>
        </React.Fragment>
      </Router>
    );
  }
}

export default App;

【问题讨论】:

    标签: javascript reactjs firebase react-router firebase-authentication


    【解决方案1】:

    会发生什么:

    • 网页加载
    • Firebase 启动请求以检查用户是否已通过身份验证
    • 应用呈现,currentUser 为空
    • 身份验证状态发生变化,但应用不会重新渲染

    应用需要监听身份验证状态的变化并相应地执行重新渲染。 https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onAuthStateChanged

    class App extends React.Component {
      state = {
        currentUser: null
      }
    
      componentDidMount() {
        this.unsubscribe = firebase.auth().onAuthStateChanged(currentUser => {
          this.setState({ currentUser })
        })
      }
    
      componentWillUnmount() {
        this.unsubscribe()
      }
    
      render() {
        // use this.state.currentUser
      }
    }
    

    【讨论】:

    • 如果您使用经过身份验证的路由并将用户重定向到登录页面,则此行为会出现问题。应用程序呈现,当前用户为空,在 firebase 有机会初始化之前重定向到登录页面。
    • 不确定我是否理解。它最初可能会显示一个登录页面,但如果用户通过身份验证,状态将更新并显示仪表板。您建议的替代方案是什么?
    • @kindaro 除了在呈现任何反应之前等待登录状态之外,我不知道任何解决方案(仍在寻找)。如果是 SPA,您提到的内容很好(刷新登录页面,然后在状态后刷新到登录页面),但是如果您将用户重定向到单独的登录 URL,那么它就会成为问题。
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 2021-03-27
    • 2021-02-15
    • 2020-11-15
    • 2019-04-27
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    相关资源
    最近更新 更多