即使用户在注册流程中经过身份验证,如何修复 firebase "User is not authorized" 错误?
How to fix firebase "User is not authorized" error even though user is authenticated in registration flow?
我正在尝试使用 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);
});
};
同样,控制台中记录的错误是:
profile image upload failed: Error: User is not authorized to perform the desired action.
在 uploading()
函数 returns 当前用户对象之前成功记录 firebase.auth().currentUser
。
这个安全规则问题也发生在 Firestore 上,尽管我对给定集合的安全规则集是:
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");*/
})
});
});
});
});
我正在尝试使用 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);
});
};
同样,控制台中记录的错误是:
profile image upload failed: Error: User is not authorized to perform the desired action.
在 uploading()
函数 returns 当前用户对象之前成功记录 firebase.auth().currentUser
。
这个安全规则问题也发生在 Firestore 上,尽管我对给定集合的安全规则集是:
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");*/
})
});
});
});
});