如何解决即使在注册流程中对用户进行了身份验证,如何解决Firebase“用户未授权”错误?
我正在尝试使用react-native-firebase在react-native应用程序的一个调用中执行多种firebase操作。流程是这样的:
- 通过身份验证创建用户
- 将图像发送到存储空间
- 将数据发送到Firestore
在图像存储阶段,imgRef.putFile()
函数会错误地指出用户未被授权。但是,即时通讯使用createUserWithEmailAndPassword()
(在完成时对用户进行身份验证),然后使用返回的凭据来完成其余的工作,图像存储和Firestore创建。
firebase存储规则设置为仅允许经过身份验证的用户。这是规则集:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read,write: if request.auth != null;
}
}
}
此外,我已经在身份验证方法中启用了匿名登录。
这是直到错误点的初始动作:
return (dispatch) => {
dispatch({ type: types.REGISTER_USER });
console.log('starting registration process...');
firebase
.firestore()
.collection('users')
.where('username','==',username)
.get()
.then((querySnapshot) => {
console.log(querySnapshot);
if (querySnapshot.empty !== true) {
registrationFail(dispatch,'Username already taken. Try again.');
console.log("Registrant's username already exists");
} else {
console.log('Registrants username is unique');
firebase
.auth()
.createUserWithEmailAndPassword(email,pass)
.then((userCredential) => {
uploadImg(dispatch,img,userCredential.user.uid)
这是uploadImg()函数:
const uploadImg = async (dispatch,uri,uid) => {
console.log('Starting image upload...');
dispatch({ type: types.UPLOAD_IMG,info: 'Uploading profile image...' });
const uploadUri = Platform.OS === 'ios' ? uri.replace('file://','') : uri;
const imgRef = firebase.storage().ref('profile-images').child(uid);
return imgRef
.putFile(uploadUri,{ contentType: 'image/png' })
.then((success) => {
console.log('profile image upload successful.')
return imgRef;
})
.catch((err) => {
console.log('profile image upload failed: ' + err)
uploadImgFail(dispatch,err.message);
});
};
再次,在控制台中记录的错误是:
个人资料图片上传失败:错误:用户无权执行所需的操作。
在firebase.auth().currentUser
函数之前登录uploading()
成功返回当前用户对象。
尽管我的给定集合的安全规则集为:
match /databases/{database}/documents {
match /users/{uid} {
// allow new user to check phone numbers
allow read: if true
allow update,create: if request.auth != null
allow delete: if request.auth.uid == uid
}
}
这是我的注册流程的一部分。我收集输入,将相关数据发送到redux动作,创建一个用户,一旦创建了该用户,便向Firestore中的文档添加一些数据。这没有任何意义。我正在引用文档,但仍然无法使用。
如何解决?
解决方法
firebase似乎有一个问题,您必须先 logout()用户一次,然后才能在创建用户后登录。我遇到了同样的问题,这是我的解决方法:
firebase.auth().signInWithEmailAndPassword(userEmail,userPass).catch((error) => {
/*console.log('Could not log in user');*/
console.log(error);
firebase.auth().createUserWithEmailAndPassword(userEmail,userPass).catch((error) => {
/*console.log('Could not create user');*/
console.log(error);
}).then(result => {
firebase.auth().signOut().then(() => {
firebase.auth().signInWithEmailAndPassword(userEmail,userPass).then(result => {
/*console.log("here is you logged in user");*/
})
});
});
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。