[英]Firebase and React Native: onAuthStateChanged doesn't get triggered
So I'm developing my very first React native app with Firebase and I have the following structure in my code:因此,我正在使用 Firebase 开发我的第一个 React 本机应用程序,并且我的代码中有以下结构:
ComponentDidMount(){
this.verifySession();
console.log("Component did mount");
}
verifySession = () =>{
console.log("inside VS");
firebase.auth().onAuthStateChanged(user =>{
if(user){
this.props.navigation.navigate('Dashboard');
console.log("dashboard");
}else{
this.props.navigation.navigate('Login');
console.log("Login");
}
});
}
Problem is, my verifySession function is not getting triggered.问题是,我的 verifySession function 没有被触发。 Actually, I'm not getting any logs so I think ComponentDidMount is not being executed either.
实际上,我没有收到任何日志,所以我认为 ComponentDidMount 也没有被执行。 This code is placed within my LoadingScreen component.
此代码放置在我的 LoadingScreen 组件中。
Here's my app.js where I initialize the Firebase conf:这是我的 app.js,我在其中初始化 Firebase conf:
import * as firebase from 'firebase';
import {firebaseConfig} from './data/config';
const Stack = createStackNavigator();
firebase.initializeApp(firebaseConfig);
I've also tried the following for my LoadingScreen:我还为我的 LoadingScreen 尝试了以下方法:
import Dashboard from './dashboard';
import Login from './login';
render(){
firebase.auth().onAuthStateChanged(function(user){
if(user){
return (<Dashboard/>);
}else{
return (<Login/>);
}
});
return(
<View>
<Text>Verifying session...</Text>
</View>
)
}
Login should get rendered but instead it shows "Verifying session" which makes me think if the statement is not getting executed at all.登录应该被渲染,但它显示“验证会话”,这让我想如果语句根本没有被执行。
Any thoughts/recommendations on this matter will be appreciated.对此问题的任何想法/建议将不胜感激。
onAuthStateChanged
is meant to add an observer and not to be called directly. onAuthStateChanged
旨在添加观察者而不是直接调用。 Could be it's not firing because the user's login-status is not changing at all and you're not attempting to sign-in anyone in, so there's nothing to trigger it.可能它没有触发,因为用户的登录状态根本没有改变,而且您没有尝试让任何人登录,所以没有什么可以触发它。 Try implementing
auth().createUserWithEmailAndPassword(email, password)
or `auth().signInWithEmailAndPassword(email, password)' and test out user creation/sign-in.尝试实现
auth().createUserWithEmailAndPassword(email, password)
或 `auth().signInWithEmailAndPassword(email, password)' 并测试用户创建/登录。
This article may also be of use;这篇文章也可能有用; it puts an auth observer in a separate component and allows you to navigate between two navigation stacks based on user's login status: https://heartbeat.fritz.ai/how-to-manage-authentication-flows-in-react-native-with-react-navigation-v5-and-firebase-860f57ae20d3
它将身份验证观察器放在一个单独的组件中,并允许您根据用户的登录状态在两个导航堆栈之间导航: https://heartbeat.fritz.ai/how-to-manage-authentication-flows-in-react-native- with-react-navigation-v5-and-firebase-860f57ae20d3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.