为什么每次重新加载时Firestore都会创建新用户?



我正试图使一个Todo/Notes应用程序与Firebase作为DB。该应用程序与FirestoreDB完美同步,但每次我关闭并重新打开该应用程序时,该应用程序似乎删除了以前的凭据并创建了一个新用户,并且所有以前的笔记都从应用程序中消失了,这些笔记仍然存在于FirestoreDB上。谁能告诉我我哪里做错了?

这是App.js

import {
StyleSheet,
Text,
View,
TouchableOpacity,
FlatList,
Modal,
ActivityIndicator,
} from 'react-native';
import colors from './Colors';
import { AntDesign } from '@expo/vector-icons';
import TodoList from './components/TodoList';
import AddListModal from './components/AddListModal';
import Fire from './Fire';
export default class App extends React.Component {
state = {
addTodoVisibile: false,
lists: [],
user: {},
loading: true,
};
componentDidMount() {
firebase = new Fire((error, user) => {
if (error) {
return alert('Something went wrong');
}
firebase.getLists(lists => {
this.setState({ lists, user }, () => {
this.setState({ loading: false });
});
});
this.setState({ user });
});
}
componentWillUnmount() {
firebase.detach();
}
toggleAddTodoModal() {
this.setState({ addTodoVisibile: !this.state.addTodoVisibile });
}
renderList = list => {
return <TodoList list={list} updateList={this.updateList} />;
};
addList = list => {
firebase.addList({
name: list.name,
color: list.color,
todos: [],
});
};
updateList = list => {
firebase.updateList(list);
};
deleteList = list => {
firebase.deleteList(list);
};
renderList = list => {
return (
<TodoList
list={list}
updateList={this.updateList}
deleteList={this.deleteList}
/>
);
};
render() {
if (this.state.loading) {
return (
<View style={styles.container}>
<ActivityIndicator size={50} color={colors.highlight} />
</View>
);
}
return (
<View style={styles.container}>
<Modal
animationType='slide'
visible={this.state.addTodoVisibile}
onRequestClose={() => this.toggleAddTodoModal()}
>
<AddListModal
closeModal={() => this.toggleAddTodoModal()}
addList={this.addList}
/>
</Modal>
<View style={{ flexDirection: 'row' }}>
<View style={styles.divider} />
<Text style={styles.title}>
Post
<Text
style={{
fontWeight: '500',
color: colors.highlight,
}}
>
It
</Text>
</Text>
<View style={styles.divider} />
</View>
<View style={{ marginVertical: 48 }}>
<TouchableOpacity
style={styles.addList}
onPress={() => this.toggleAddTodoModal()}
>
<AntDesign name='plus' size={25} color={colors.white} />
</TouchableOpacity>
<Text style={styles.add}>Add a List</Text>
</View>
<View style={{ height: 275 }}>
<FlatList
data={this.state.lists}
keyExtractor={item => item.id.toString()}
horizontal={true}
showsHorizontalScrollIndicator={false}
renderItem={({ item }) => this.renderList(item)}
keyboardShouldPersistTaps='always'
/>
</View>
<Text style={styles.delete}>
<Text style={styles.highlight}>(</Text>
Long Press on List to Delete
<Text style={styles.highlight}>)</Text>
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: colors.background,
alignItems: 'center',
justifyContent: 'center',
},
divider: {
backgroundColor: colors.highlight,
height: 1,
flex: 1,
alignSelf: 'center',
},
title: {
fontSize: 38,
fontWeight: '800',
color: colors.white,
paddingHorizontal: 64,
},
addList: {
borderWidth: 2,
borderColor: colors.highlight,
borderRadius: 4,
padding: 16,
alignItems: 'center',
},
add: {
color: colors.white,
fontWeight: '600',
fontSize: 16,
marginTop: 8,
},
delete: {
color: colors.white,
fontWeight: '500',
fontSize: 12.5,
position: 'absolute',
bottom: 8,
},
highlight: {
color: colors.highlight,
},
});

这是Fire.js

import 'firebase/compat/firestore';
import 'firebase/compat/auth';
const firebaseConfig = {

};
class Fire {
constructor(callback) {
this.init(callback);
}
init(callback) {
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig);
}
firebase.auth().onAuthStateChanged(user => {
if (user) {
callback(null, user);
} else {
firebase
.auth()
.signInAnonymously()
.catch(error => {
callback(error);
});
}
});
}
getLists(callback) {
let ref = this.ref.orderBy('name');
this.unsubscribe = ref.onSnapshot(snapshot => {
lists = [];
snapshot.forEach(doc => {
lists.push({ id: doc.id, ...doc.data() });
});
callback(lists);
});
}
addList(list) {
let ref = this.ref;
ref.add(list);
}
updateList(list) {
let ref = this.ref;
ref.doc(list.id).update(list);
}
deleteList(list) {
let ref = this.ref;
ref.doc(list.id).delete();
}
get userId() {
return firebase.auth().currentUser.uid;
}
get ref() {
return firebase
.firestore()
.collection('users')
.doc(this.userId)
.collection('lists');
}
detach() {
this.unsubscribe();
}
}
export default Fire;

在大多数环境下,Firebase Authentication SDK会自动保存用户凭证,并在重新加载应用/页面时恢复它们,然后调用您的onAuthStateChanged处理程序。

在React Native上,默认情况下这是不可能的,所以我建议查看Firebase关于管理认证持久性的文档。

最新更新