【发布时间】:2023-03-20 15:36:01
【问题描述】:
正如标题所说。 我需要将经过身份验证的用户链接到数据库。 这样不同的用户只能看到他们的数据。
我已经成功实现了 Firebase 的 Authentication 功能。 但是记录没有保存到实时数据库,我不知道该怎么做。
谁能教我怎么做这样的功能? 我尝试了数据库,我大致知道它使用(推送)创建一个唯一 ID 来区分用户。但我仍然不知道如何将其与身份验证功能结合起来。
以下代码是我的Login()和Register()函数
login (){
const validate = this.refs.formId.getValue();
if (validate) {
firebase.auth().signInWithEmailAndPassword(validate.email, validate.password)
.then(() => {
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
Toast.show({ text: `${Strings.ST30}`, position: 'bottom', buttonText: `${Strings.ST33}` })
}
else if (errorCode === 'auth/user-not-found') {
Toast.show({ text: `${Strings.ST31}`, position: 'bottom', buttonText: `${Strings.ST33}` })
}
else{
Toast.show({ text: `${Strings.ST32}`, position: 'bottom', buttonText: `${Strings.ST33}` })
}
});
}
}
register () {
const validate = this.refs.form.getValue();
if(this.validate) {
const errorHandler = ((e)=>{
console.log(e);
if(e.code == 'auth/email-already-in-use'){
Toast.show({ text: `${Strings.ST36}`, position: 'bottom', buttonText: `${Strings.ST33}` })
} else {
Toast.show({ text: `${Strings.ST32}`, position: 'bottom', buttonText: `${Strings.ST33}` })
}
})
firebase.auth().createUserWithEmailAndPassword(validate.email,validate.password).then((response) => {
firebase.auth().currentUser.updateProfile({
displayName : validate.name,
}).then(()=>{
}).catch(errorHandler);
}).catch(errorHandler)
}}
非常感谢您的时间和帮助。非常感谢!
编辑 2:
firebase.auth()
.createUserWithEmailAndPassword(validate.email,validate.password)
.then((response) => {
firebase.auth().currentUser.updateProfile({
displayName : validate.name,
}).then(()=>{
firebase.database()
.ref('users/' + firebase.auth().currentUser.uid + "/profile")
.set({
Username: validate.name,
UserEmail: validate.email,
CreationDate: d1.getFullYear() + "/" + (d1.getMonth() + 1) + "/" + d1.getDate()
})
}).catch(errorHandler);
}).catch(errorHandler)
}}
但我无法从我的数据库中写入和读取其他函数。 这是我对 Firebase 数据库的规则。我可以知道我应该改变哪个部分吗?
{
"rules": {
".read": false,
".write": false,
"users":
{
"$userId":
{
".read": "auth.uid === $userId",
".write": "auth.uid === $userId"
}
}
}
}
编辑 3:(ExampleSendData.js)
构造函数(道具){
超级(道具)
这个.state = {
信息: '',
消息:[]
}
this.addItem = this.addItem.bind(this); }
componentDidMount() {
firebase
.database()
.ref()
.child("messages")
.once("value", snapshot => {
const data = snapshot.val()
if (snapshot.val()) {
const initMessages = [];
Object
.keys(data)
.forEach(message => initMessages.push(data[message]));
this.setState({
messages: initMessages
})
}
});
firebase
.database()
.ref()
.child("messages")
.on("child_added", snapshot => {
const data = snapshot.val();
if (data) {
this.setState(prevState => ({
messages: [data, ...prevState.messages]
}))
}
})
}
addItem () {
if (!this.state.message) return;
const newMessage = firebase.database().ref()
.child("messages")
.push();
newMessage.set(this.state.message, () => this.setState({message: ''}))
}
render() {
return (
<View style={styles.container}>
<View style={styles.msgBox}>
<TextInput placeholder='Enter your message'
value={this.state.message}
onChangeText={(text) => this.setState({message: text})}
style={styles.txtInput}/>
<Button title='Send' onPress={this.addItem}/>
</View>
<FlatList data={this.state.messages}
renderItem={
({item}) =>
<View style={styles.listItemContainer}>
<Text style={styles.listItem}> {item}
</Text>
</View>
}
/>
</View>
);
}
}
【问题讨论】:
标签: firebase react-native firebase-realtime-database firebase-authentication react-native-android