如何从 Firebase 文档中读取值并与变量进行比较,然后更改文档的值



我正在尝试让用户使用验证码确认他们的帐户。我想从 firestore db 获取用户文档,检查以确保身份验证代码与提供的值匹配,然后将文档的已验证字段更改为 True。

这是针对移动应用程序(在设备上,而不是服务器端),所以我不能使用 firebase-admin...我出现了一个屏幕,但是一旦我填写了身份验证字段,单击按钮就不会发生任何操作,但是我可以确认该功能肯定已到达,只是由于某些错误而未执行其中的代码。

handleConfirmation = () => {
const auth_code = this.state.authCode;
let user = firebase.firestore().collection('users').where('uid', '==', firebase.auth().currentUser.uid);
// ^ I am not sure if this is correct... could be a source of wrongness.
if (user.exists === true) {
console.log(user.data());
let user_to_verify = user.data();
const has_verified = user_to_verify.hasVerified;
if (has_verified === false) {
const user_auth_code = user.authCode;
if (auth_code === user_auth_code) {
console.log("User verification code is correct");
this.setState({hasVerified: true});
this.updateUser();
// ^ this function should set the 
// value of user.hasVerified to True, and 
// save it in firestore (aka firebase firestore) 
//
// Now the user can successfully login to app 
}
}else{
// user doesnt exist... throw error and exit
}

提交表单时(在应用程序中按下按钮时),将执行handleConfirmation,并将auth_code与user_auth_code(这是Firebase Firestore文档中authCode字段的值)进行比较,如果这些值匹配,则用户的hasVerified 字段将更改为True并保存在Firebase中。

请帮忙!仅供参考,这是我在StackOverFlow上的第一篇文章,所以如果我遵循了正确的准则,请告诉我。

编辑:显示我在创建时如何初始化用户。

constructor() {
super();
this.ref = firebase.firestore().collection('users');
this.state =
{
firstname: '<first name>',
lastname: '<last name>',
email: '<email>',
password: '<password>',
errorMessage: '<none unless error occurs>',
secureTextEntry: true,
confirmPassword: '<password>',
modalVisible: false,
imageURI: '<some url>',
authCode: '<authentication code>',
hasVerified: false,
};
this._keyboardDidHide = this._keyboardDidHide.bind(this);
this.setDate = this.setDate.bind(this);
}
.
.  // SKIPPED SOME IN-BETWEEN LINES FOR BREVITY
.
updateUser() {
let user_data = {
uid: firebase.auth().currentUser.uid,
firstname: this.state.firstname,
lastname: this.state.lastname,
email: this.state.email,
imageURI: this.state.imageURI,
authCode: this.state.authCode,
hasVerified: this.state.hasVerified,
};
console.log(user_data);
this.ref.doc(firebase.auth().currentUser.uid).set(user_data);
this.props.navigation.navigate('homescreen');
}

查看下面的代码,

您必须将文档的文档 ID 存储在文档中,以便在后期阶段更新用户。我在最后举了一个例子来说明如何做到这一点。

handleConfirmation = () => {
const auth_code = this.state.authCode;
var user = firebase
.firestore()
.collection("users")
.where("uid", "==", firebase.auth().currentUser.uid)
.get()
.then(querySnapshot => {
if (querySnapshot._docs.length > 0) { // User exists !!
console.log(querySnapshot._docs);
// You require the doc_Id of the document so that you can update it in the later stage.
const has_verified = querySnapshot._docs[0]._data.hasVerified; //_docs is a array, considering there is only 1 unique user
if (has_verified == false) {
const user_auth_code = querySnapshot._docs[0]._data.authCode; // or use firebase.auth().currentUser.uid instead.
if (auth_code === user_auth_code) {
console.log("User verification code is correct");
this.setState({ hasVerified: true });
this.updateUser(querySnapshot._docs[0]._data.doc_Id); // As told above doc_ID is required
}
}
}
});
};
updateUser = doc_id => {
var user = firebase
.firestore()
.collection("users")
.doc(doc_id)
.set({
hasVerified: true
});
};
//Example for adding doc_ID in document during document creation. Make sure you do this process during user creation.
//The below code is for your reference.
exampleDocCreate = () => {
var user = firebase
.firestore()
.collection("users")
.add({
userName: "React Native User"
})
.then(data => {
var user = firebase
.firestore()
.collection("users")
.doc(data.id)
.set({
doc_id: data.id
});
});
};

根据我的理解,您正在寻找一种方法, 1) 查找存在的用户。 2)如果存在,请获取其已验证和授权代码信息。 3) 比较和更新集合中的文档。

我希望我能帮助你

相关内容

  • 没有找到相关文章

最新更新